September 24, 2014 · JS
Javascript - 01 - Variables, Data Type
Javascript has different version because of the different standard.
JavaScript version | Navigator version |
---|---|
JavaScript 1.0 | Navigator 2.0 |
JavaScript 1.1 | Navigator 3.0 |
JavaScript 1.2 | Navigator 4.0-4.05 |
JavaScript 1.3 | Navigator 4.06-4.7x |
JavaScript 1.4 | |
JavaScript 1.5 | Navigator 6.0 Mozilla (open source browser) |
JavaScript 1.6 | Firefox 1.5, other Mozilla 1.8-based products |
JavaScript 1.7 | Firefox 2, other Mozilla 1.8.1-based products |
JavaScript 1.8 | Firefox 3, other Gecko 1.9-based products |
Data types
Type | Examples of typed values / Notes |
---|---|
Numbers | 42, 3.14159 |
Logical (Boolean) | true / false |
Strings | "Howdy" |
null | a special keyword denoting a null value; null is also a primitive value. Because JavaScript is case-sensitive, null is not the same as Null , NULL , or any other variant |
undefined | a top-level property whose value is undefined; undefined is also a primitive value. |
Define variables
var x = 10; //local variables. y = 10; //global variables.
Variable default value is undefined.
NaN is a property of the global object.
NaN === NaN; // false Number.NaN === NaN; // false isNaN(NaN); // true isNaN(Number.NaN); // true
The initial value of NaN is Not-A-Number — the same as the value of Number.NaN. In modern browsers, NaN is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it.
Constant
const prefix = '212';
Array
var fish = ["Lion", , "Angel"];
Object Literals
var Sales = "Toyota"; function CarTypes(name) { if (name == "Honda") { return name; } else { return "Sorry, we don't sell " + name + "."; } } var car = { myCar: "Saturn", getCar: CarTypes("Honda"), special: Sales }; console.log(car.myCar); // Saturn console.log(car.getCar); // Honda console.log(car.special); // Toyota /*=================================Nested Object=========================*/ var car = { manyCars: {a: "Saab", "b": "Jeep"}, 7: "Mazda" }; console.log(car.manyCars.b); // Jeep console.log(car[7]); // Mazda /*===========================Empty String Identifier===================*/ var unusualPropertyNames = { "": "An empty string", "!": "Bang!" } console.log(unusualPropertyNames.""); // SyntaxError: Unexpected string console.log(unusualPropertyNames[""]); // "An empty string" console.log(unusualPropertyNames.!); // SyntaxError: Unexpected token ! console.log(unusualPropertyNames["!"]); // "Bang!" //The empty string Identifier is able to compiled , but not as used. /*=========================Unusual Case on Selector==================*/ var foo = {a: "alpha", 2: "two"}; console.log(foo.a); // alpha console.log(foo[2]); // two //console.log(foo.2); // Error: missing ) after argument list //console.log(foo[a]); // Error: a is not defined console.log(foo["a"]); // alpha console.log(foo["2"]); // two
Special Characters
Character | Meaning |
---|---|
\b |
Backspace |
\f |
Form feed |
\n |
New line |
\r |
Carriage return |
\t |
Tab |
\v |
Vertical tab |
\' |
Apostrophe or single quote |
\" |
Double quote |
\\ |
Backslash character |
\XXX |
The character with the Latin-1 encoding specified by up to three octal digits XXX between 0 and 377. For example, \251 is the octal sequence for the copyright symbol. |
\xXX |
The character with the Latin-1 encoding specified by the two hexadecimal digits XX between 00 and FF. For example, \xA9 is the hexadecimal sequence for the copyright symbol. |
\uXXXX |
The Unicode character specified by the four hexadecimal digits XXXX. For example, \u00A9 is the Unicode sequence for the copyright symbol. SeeUnicode escape sequences. |