notes
//Single line comment /* This is a multiline comment multiline comment */ /*···/* Nested comments */···*/
statement
-
Var: Declare a variable, which can be initialized to a value optionally. -
Let: Declare a block scope local variable, which can be initialized to a value optionally. -
Const: Declare a read-only named constant.
variable
Declare variables
-
var x = 42。 This syntax can be used to declare both local and global variables. -
x = 42。 This will declare a global variable and cause a strict warning when compiling JavaScript. Therefore, you should avoid using this unconventional format. -
let y = 42。 This syntax can be used to declare the block scope local variable of the statement block code segment.
Evaluate variables
var a; console.log("The value of a is " + a); // logs "The value of a is undefined" console.log("The value of b is " + b); // throws ReferenceError exception
var input; if(input === undefined){ doThis(); } else { doThat(); }
var myArray = new Array(); if (!myArray[0]) myFunction();
var a; a + 2; // Evaluates to NaN
var n = null; console.log(n * 32); // logs 0
Field of variable
if (true) { var x = 5; } console.log(x); // five
if (true) { let y = 5; } console.log(y); // ReferenceError: y is not defined
Variable declaration promotion
/** * Example 1 */ console.log(x === undefined); // logs "true" var x = 3; /** * Example 2 */ // will return a value of undefined var myvar = "my value"; (function() { console.log(myvar); // undefined var myvar = "local value"; })();
/** * Example 1 */ var x; console.log(x === undefined); // logs "true" x = 3; /** * Example 2 */ var myvar = "my value"; (function() { var myvar; console.log(myvar); // undefined myvar = "local value"; })();
constant
const prefix = '212';
// THIS WILL CAUSE AN ERROR function f() {}; const f = 5; // THIS WILL CAUSE AN ERROR ALSO function f() { const g = 5; var g; //statements }
Data structure and type
There are six types of prototype data: -
Boolean. Boolean values, true and false -
\null. A special keyword indicating null value. JavaScript is case sensitive, so null is completely different from null, NULL, or other variables. -
\undefined. Property when the variable is undefined. -
Number. Represents a number, such as 42 or 3.14159. -
String. Represents a string, for example: "Howdy" -
Symbol (newly added type in ECMAScript 6). A data type whose instances are unique and immutable.
-
-
Object
Conversion of data types
var answer = 42;
answer = "Thanks for all the fish...";
x = "The answer is " + 42 // "The answer is 42" y = 42 + " is the answer" // "42 is the answer"
"37" - 7 // 30 "37" + 7 // "377"
Convert string to number
ParseInt() and parseFloat()
parseInt(string, radix);
parseInt(" 0xF", 16); parseInt(" F", 16); parseInt("17", 8); parseInt(021, 8); parseInt("015", 10); parseInt(15.99, 10); parseInt("15,123", 10); parseInt("FXX123", 16); parseInt("1111", 2); parseInt("15*3", 10); parseInt("15e2", 10); parseInt("15px", 10); parseInt("12", 13);
parseInt("Hello", 8); // Not a number at all parseInt("546", 2); // Digits are not valid for binary representations
parseFloat(string)
parseFloat("3.14"); parseFloat("314e-2"); parseFloat("0.0314E+2"); parseFloat("3.14more non-digit characters");
parseFloat("FF2");
Monocular addition operator
"1.1" + "1.1" = "1.11.1" (+"1.1") + (+"1.1") = 2.2 // Note: the parentheses are added for clarity, not required.