1) Basic Concepts
a) Statement (statement) is the action that is performed to accomplish a task, with a semicolon ending with a semicolon representing the end of the statement. such as: var a = 1;
b) Expression , which is a formula for getting the return value. such as: 1+3
c) A variable is a reference to a "value", using a variable equivalent to referencing a value.var a = 1;变量存在声明和赋值两个过程,var a = 1;等价于 var a;a = 1;
It is stated below that an existing variable is invalid,
var x = 1;
var x;
x // 1
However, if the second declaration is also assigned a value, the preceding value is overwritten.
var x = 1; var x = 2; Equivalent to var x = 1; var x; x = 2
< Span class= "NX" >
< Span class= "NX" > The Avascript engine works by parsing the code, getting all the declared variables, and then running it one line at a line. The result is that all declarations of variables will be promoted to the head of the code, which is called variable elevation.
/span>
console.log(a);var a = 1;
Equivalent to
var a;console.log(a);a = 1;
< Span class= "NX" > < Span class= "Mi" > note that the variable promotion is valid only for variables declared by the var
command, if a variable is not var
If the command is declared, the variable promotion will not occur. The
D) identifier (identifier) is a name used to identify a specific object. The most common identifier is the variable name, and the name of the function to be mentioned later. The identifier for the JavaScript language is case sensitive, so a
and a
are two different identifiers.
/span>
- The first character, which can be any Unicode letter, as well as the dollar sign (
$
) and underscore ( _
).
- The second and subsequent characters, in addition to Unicode letters, dollar signs, and underscores, can also be numbered
0-9
.
e) The data types are as follows (it is said that ES6 has added new data types, which are not discussed here):
- Value (number): integers and decimals (e.g. 1 and 3.14)
- String (String): text consisting of characters (e.g. "Hello World")
- Boolean value (Boolean):
true
(True) and false
(false) two specific values
- Undefined: Indicates "undefined" or does not exist, that is, there is currently no value here
- Null: Indicates a vacancy, that is, there should be a value here, but is currently empty
- Object: A collection of various values
f) An object can be divided into three subtypes.
- Narrow objects (object)
- Arrays (Array)
- Functions (function)
g) typeof operator ( typeof
operator can return the data type of a value)
typeof 123 // "number"typeof ‘123‘ // "string"typeof false // "boolean"
function f() {}typeof f// "function"
typeof undefined// "undefined"
typeof window // "object"typeof {} // "object"typeof [] // "object"typeof null // "object"
既然 typeof
Arrays and objects (object) display the results object
, so how do you differentiate them?
h) instanceof operator (for instances of a type)
var o = {};var a = [];o instanceof Array // falsea instanceof Array // true
i) null and undefined
null
And undefined
both can mean "no", meaning very similar. Assign a variable to undefined
or, to be null
honest, the grammatical effect is almost indistinguishable.
First, NULL is treated as an object, as in Java. However, the data type of JavaScript is divided into two categories: primitive type and composition type, Brendan eich think that the value of "none" is best not an object.
Second, the initial version of JavaScript does not include the error handling mechanism, when data type mismatch occurs, is often the type of automatic conversion or silently fail. Brendan Eich felt that if the null
automatic switch to 0, it is not easy to find errors.
Therefore, Brendan Eich also designed a undefined
. He distinguishes between: an null
object that represents "none", a value of 0 when converted to a number, or undefined
a primitive value that represents "none", or a value when converted to a number NaN
.
Tenth JavaScript Basic syntax