Variable var const let
Variables increase the scope
Variables declared by const lets are not promoted in scope -> temporary dead zone Const lets cannot be declared repeatedly and are not an attribute of window objects const declaration restrictions apply only to references to variables to which they point
for (const key in {a:1, b:2}) {
console.log(key)
}
// a, b
for (const key of [1, 2, 3, 4, 5]) {
console.log(key)
}
// 1, 2, 3, 4, 5,
Copy the code
Statement style and best Practices:
- Do not use the var
- Const takes precedence over let
The data type
Data type judgment
7 basic data types: Undefined Null Boolean Number String Symbol Object
The typeof operator
- “undefiend”
- “boolean”
- “string”
- “number”
- “object”
- “function”
- “symbol”
type of null ?
Copy the code
Undefined
Note: there is a difference between including undefined variables and undefined variables
let message; // let age console.log(message); // "undefined" console.log(age); / / an errorCopy the code
For undeclared variables, only one useful operation, Typeof, can be performed
Let message // Make sure this variable is not declared // let age console.log(typeof message); // "undefined" console.log(typeof age); // "undefined"Copy the code
Undefiant is a false value
Null
Null object Pointers are recommended to be initialized with NULL when defining the object value variable to hold in the future
typof null === 'object'
null == undefined
Copy the code
Boolean
The data type | The value converted to true | Convert to a value of false |
---|---|---|
Boolean | true | false |
String | Non-empty string | “” (empty string) |
Number | Non-zero values (including infinity) | 0, NaN |
Object | Any object | |
Undefined | N/A(non-existent) | undefined |
Number
Because using floating-point values uses twice as much memory as storing integers, ECMAScript always tries to convert values to the integer E
0.1 + 0.2 === 0.3?Copy the code
The range of values
- Number.MAX_VALUE
- Number.MIN_VALUE
- isFinite()
- Infinity
- -Infinity
NAN
IsNaN () type conversion
Not a Number
console.log(5/0); // 0
console.log(0/0); // NaN
console.log(-0/+0); // NaN
console.log(5/0); // Infinity
console.log(5/-0); // -Infinity
Copy the code
Numerical transformation
Number() ParseInt() ParseFloat()
Number()
- Boolean values that convert true to 1 and false to 0
- Value, return directly
- Null, returns 0
- Undefined, returns NaN
- String, “string returns 0
- Object, the valueof() method, and converts the returned value according to the above rules. If the result is NaN, the toString () method is called and converted according to the conversion string method
parseInt(), parseFloat()
- Converts from the first non-space character
- If the first character is not a numeric character, add or subtract, and return NaN
String
Convert to string
- If the value has a toString() method, that method is called (with no arguments) and the result is returned
- null, “null”
- undefined, “undefined”
A numeric call to toString() can pass arguments