Declare variables, keyword: var
1 // declare a variable 2 var name; . 3 . 4 // assign values to variables . 5 name = 'Husky' ; . 6 . 7 // declare a variable and assignment . 8 var name = 'Husky';
Use variables
1 // declare a variable assignment and 2 var Age =. 17 ; . 3 . 4 // use variables - Printing 5 console printout . 6 the console.log ( 'my age is:' + Age); . 7 . 8 content of printed pages . 9 Document .write ( 'my age is:' + age);
Naming variables
1, the variable is underlined letters, numbers, $, letters
2, can not be digital beginning
3, can not be keywords and reserved words
Naming variables
1 , the variable name naming common with Hungarian notation , hump nomenclature and Pa ska nomenclature
2, try to avoid using the name does not make sense
3, case sensitive
4, named to be meaningful in order to see the name EENOW
1 // hump nomenclature 2 var bigBook = 'Journey' ; . 3 . 4 // Pascal nomenclature . 5 var Hungarian = 'the Hello' ; . 6 . 7 // Hungarian notation . 8 var EndName = 'Husky';
On board Case: swap two variables
Case I: The third variable exchange
1 // declare a variable assignment 2 var num1 =. 5, num2 =. 6 , TEMP; . 3 . 4 // Start exchanged . 5 TEMP = num2; . 6 num2 = num1; . 7 num1 = TEMP; . 8 . 9 // output 10 Console. log ( 'after two variable exchange, num1:' + + num1 '; num2:' + num2); . 11 12 is // results 13 two variable exchange after, num1: 6; num2: 5
Case II: two variable exchange
1 // declare a variable assignment 2 var num1 =. 5, num2 =. 6 ; . 3 . 4 // two numbers exchanged subtraction . 5 var num1 num2 = num1 +; // num1 =. 11 . 6 var num2 = num1 - num2; / / num2 = 11-6 =. 5 . 7 var num1 = num1 - num2; // num1 = 11-5 =. 6 . 8 . 9 // output 10 'after the exchange of two variables, num1:' console.log (+ num1 + '; num2 : '+ num2); . 11 12 is // result 13 after the two variable exchange, num1: 6; num2: 5