Declarations
The undefined value will be treated as false in a Boolean environment.
The undefined value in the numerical type environment will be converted to NaN.
When evaluating an empty variable, the null value null will be treated as 0 in the numerical type environment, and the boolean type environment will be treated as false.
JavaScript variables feel "promoted" or moved before all functions and statements. The promoted variable will return an undefined value, so even if there are declaration and initialization operations after using or referring a variable, this promoted reference will still obtain an undefined value.
Due to the existence of variable declaration promotion, all var statements in a function should be placed as close to the top of the function as possible. This greatly improves the clarity of the program code.
(In ECMAScript 2015, let(const) will not raise the variable to the top of the code block. Therefore, referencing this variable before the variable is declared will throw an error ReferenceError. This variable will be in a "temporary dead zone" from the beginning of the code block until the variable is declared.)
For functions, only function declarations are promoted to the top, excluding function expressions.
/* Function declaration */ foo(); // "bar" function foo() { ("bar"); } /* Function Expression A function defined by an expression becomes an anonymous function. Anonymous functions have no function improvement. */ baz(); // TypeError: baz is not a function //The "baz" at this time is equivalent to a declared variable, with type undefined.becausebazJust equivalent to a variable,Therefore, the browser believes"baz()"Not a function。 var baz = function() { ("bar2"); };
The global object is a window, and you can access variables declared in another window or frame from one window or frame by specifying the name of the window or frame. For example, imagine a variable called phoneNumber is declared in a document, which you can use in a subframe to reference.
Constants
In the same scope, constants cannot be named with the same name as variables or functions. However, object properties are not protected, so there is no problem with the execution of the following statement.
const MY_OBJECT = {"key": "value"}; MY_OBJECT.key = "otherValue";
Data structures and types
Six types of data types are prototypes and Object objects
- Boolean. Boolean, true and false.
- A special keyword indicating the null value. JavaScript is case sensitive, so null is completely different from Null, NULL, or other variables.
- Properties when variables are not defined.
- Number. Represents a number, for example: 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.
Objects and functions are the other two basic elements of this language. You can think of objects as named containers that hold values, and functions as procedures that your application can perform.
In numeric and string expressions involving the addition operator (+), JavaScript converts numeric values into strings.
x = "The answer is " + 42 // "The answer is 42" y = 42 + " is the answer" // "42 is the answer"
When other operators are involved (translator'-', the following minus sign '-'), the JavaScript language does not turn numbers into strings. For example (Translator's note: the first example is mathematical operation, and the second example is string operation):
"37" - 7 // 30 "37" + 7 // "377"
Converting strings to numbers
parseInt() and parseFloat()
Another way to convert a string to a number is to use the monocular addition operator.
"1.1" + "1.1" = "1.11.1" (+"1.1") + (+"1.1") = 2.2 // Note: Adding brackets is not necessary for clarity.
The object attribute name can be any string, including an empty string. If the object property name is not a legal javascript identifier, it must be wrapped with "". If the name of the attribute is illegal, then the attribute value cannot be accessed with ., but accessed and assigned through the class array tag ("[]").
var unusualPropertyNames = { "": "An empty string", "!": "Bang!" } (unusualPropertyNames.""); // Syntax error: Unexpected string(unusualPropertyNames[""]); // An empty string (unusualPropertyNames.!); // Syntax error: Unexpected token!(unusualPropertyNames["!"]); // Bang!
Notice:
var foo = {a: "alpha", 2: "two"}; (); // alpha (foo[2]); // two //(foo.2); // Error: missing ) after argument list //(foo[a]); // Error: a is not defined (foo["a"]); // alpha (foo["2"]); // two
In ES2015, object literal extension supports setting prototypes when creating, abbreviated foo: foo allocation, defining methods, processing parent functions (super calls), and calculating property names (dynamics). In short, these also bring together object literals and class declarations, allowing object-based design to benefit from some of the same conveniences.
var obj = { // __proto__ __proto__: theProtoObj, // Shorthand for ‘handler: handler' handler, // Methods toString() { // Super calls return "d " + (); }, // Computed (dynamic) property names [ 'prop_' + (() => 42)() ]: 42 };
Special characters that can be used in JavaScript strings...
Escape characters
// Quotation escapevar quote = "He read /"The Cremation of Sam McGee/" by . Service."; // Escape backslashvar home = "c://temp"; // Escape line breaks a sentence into multiple lines of writingvar str = "this string / is broken / across multiple/ lines." (str); // this string is broken across multiplelines. // Javascript does not have "heredoc" syntax, but it can be approximated by newline escape and newline escape at the end of the line.var poem = "Roses are red,/n/ Violets are blue./n/ I'm schizophrenic,/n/ And so am I."