02-First JavaScript code

Source: Internet
Author: User
Tags naming convention string to number

In the page, we can put tag pairs in the body tag <script type=”text/javascript”></script> , <script type=”text/javascript”></script> tag pairs

<script type= "Text/javascript" ></script>
Alert statement
<script type= "Text/javascript" >       alert ("pony Brother"); </script>

The purpose of alert (translated as "alert"): pops up "warning box".

Grammar rules

The study procedure, is has the law to follow, is the procedure has the same part, these parts are one kind of stipulation, cannot change, we become: the grammar.

(1) JavaScript is not sensitive to line, indent, and space.

Note: Each statement ends with a semicolon, although the semicolon is not mandatory, but for the program to be compressed in the future, if not semicolon, after compression will not run.

(2) All the symbols are in English. such as parentheses , quotation marks, semicolons.

(3) JavaScript comments:

Single-line Comment:

// I'm a note .

Multi-line Comments:

/* Multiline     Comment 1 multiline    comment 2* /

Note: In sublime, the shortcut key for a single-line comment is ctrl+/ that the shortcut key for a multiline comment is ctrl+shift+/ .

JavaScript output information in Web page popup warning Box: Alert ("") Console output: Console.log ("")

console.log("")Represents the output in the console. Console indicates "console", and log indicates "output".

The console is in the Chrome browser's F12. The console is where engineers and programmers debug programs. Programmers often use this statement to output something to test whether the program is correct.

User input: Prompt () statement

prompt()is specifically used to pop up a dialog box that allows the user to enter.

The code is as follows:

<script type= "Text/javascript" >        var a = prompt (' What's the weather today? ');        Console.log (a); </script>

In the above code, what the user enters will be passed to the variable A.

The effect is as follows:

F12 Open the console to view the results.

In the prompt () statement, the user is a string regardless of what is entered.

The difference between alert and prompt:

Alert ("Formerly a Mountain");                // direct use, no variables required var a = prompt ("Please enter a number");   // A variable must be used to receive the value entered by the user
Direct Volume: Numbers and strings

A "direct quantity" is a constant , also known as a literal. See what, it is what.

There are 2 simple direct quantities: numbers, strings.

(1) The direct amount of the value of the expression is very simple, write up on the line, do not need any symbols. For example:

alert (886);  // 886 is a number, so no quotation marks are required. 

(2) The string is also simple, but be sure to enclose it in quotation marks. It can be words, sentences and so on.

Definition and assignment of variable variables
   var a = 100;

var is the abbreviation for English "variant" variables. Add a space to the back, and the thing behind the space is the variable name.

    • Define variables: var is a keyword used to define variables. The so-called keyword, is a special function of the small words. The keyword must be separated by a space.

    • Assignment of a variable: the equals sign assigns the value to the right of the equal sign to the left variable.

    • Variable name: we can give the variable any name.

PS: in JavaScript, always use var to define variables , which is different from C, Java and other languages (interested students can look at ES6)

A variable must be defined before it can be used. For example, we do not set variables, direct output:

<script type= "Text/javascript" >        console.log (a);   </script>

The console will have an error:

Correct wording:

  var A;   // definition    A = +;  // Assign Value    Console.log (a);  // Output

Experienced programmers will write definitions and assignments together:

var a = +;    // defined, and assigned a value ofconsole.log (a);  // Output
Naming conventions for variables

Variable names have a naming convention: they can only consist of English letters, numbers, underscores, dollar sign $, and cannot start with a number, and cannot be a JavaScript reserved word.

The following words, called reserved words, are not allowed as variable names, do not remember:
Abstract,boolean,byte,char, class, const,debugger,double, enum, Export, extends, final,float, gotoimplements, import,int, interface,long, native , package, private, protected, public,short, Static, super, synchronized, throws, transient, volatile

Uppercase letters are available and case sensitive. In other words, a and a are two variables.

    var A = +;    // Variable 1    var a = 888;    // Variable 2
Types of variables

Variables can store numbers, strings, and so on. Variables automatically determine their type based on the type of stored content.

Numeric type: Number

If a variable holds a number, then the variable is numeric.

    var a = +;            // defines a variable A, and assigns a value    of Console.log (typeof a);  // type of output a variable

typeof () means " get the type of the variable " and the syntax is:

typeof Variable

in JavaScript, as long as it is a number, it is the numeric type . Regardless of the float, floating point number (that is, decimal), regardless of size, whether positive or negative, is number type.

String Type: String
 var  a = "ABCDE" ;     var  b = "Lu Fei" ;     var  c = "123123" ;     var  d = "ha ha ha haha" ;      var  e = ""; //  empty string   Console.log ( typeof   a);    Console.log ( typeof   b);    Console.log ( typeof   C);    Console.log ( typeof   D); Console.log ( typeof  e); 
The difference between hyphenation Fuhai

The keyboard + may be a hyphen or a plus sign of a number. As follows:

   Console.log ("I" + "Love" + "You");   // Hyphen, put three separate characters, connected together    Console.log ("I + Love + You");           // Output as-is     Console.log (1+2+3);             // Output 6

Summary : If both sides of the plus sign are numeric, this is the plus. Otherwise, it is a hyphen (used to concatenate strings).

Transfer of variable value (Assignment)


   A = b;

Assigns the value of B to a A, a, or a.

Assigns the value to the right of the equal sign to the left variable, the variable to the right of the equal sign, and the value unchanged.

To give a special example:

        var a = "3";         var b = 2;        Console.log (A-b);

Effect: (Note, string-numeric = numeric)

Variable format to convert user input

What we've said in the above is that it prompt() 's designed to pop up a dialog box that lets the user enter. The important thing is that the user is a string regardless of what they enter.

parseInt(): String to Number

parseint () to convert a string to a number . Parse represents "transform", and int denotes "integer" (note Int the spelling). For example:

String to number method:

parseint ("5");

parseint () also has the following characteristics :

(1) With the function of automatic purification, only the number of the beginning of the string is preserved , the following Chinese automatically disappears. For example:

Console.log (parseint ("2018) you are so handsome!! ");

(2) Automatically with truncation decimal function: Rounding , not rounding .

var a = parseint (5.8) + parseint (4.7); Console.log (a);
var a = parseint (5.8 + 4.7); Console.log (a);

02-First JavaScript code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.