This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging
Let and const
let
Let defines variables that cannot be defined repeatedly, but can change their values
let name = 'zhangsan'; name = 'lisi'; console.log(name); // lisi let name = 'wangwu'; Error: Identifier 'name' has already been declaredCopy the code
Has block-level scope (block-level scope is braces)
{ let a = 1; console.log(); // 1 } console.log(age); For (let I = 0; let I = 0; i < 10; I ++) {// I can only be used within this scope because of the block-level scope} console.log(I); // There is no definition of I in this scopeCopy the code
It is not pre-parsed, simply defined before use
console.log(a); let a = 0; // Error: a is not definedCopy the code
Let declared variables are not pushed into the Window object and are independent
let a = 100;
console.log(window.a); // undefined
Copy the code
Note:If a variable is declared using var, it cannot be declared again using let, and vice versa. The reason is that this variable has already been declared.
const
Use the const keyword to define a constant. A constant is immutable. Once defined, its value cannot be modified
// 1. Const PI = 3.1415926; // 2. Const PI = 3.1415926; PI = 3.14; Once initialized, it cannot be modifiedCopy the code
When you initialize a constant, you must give an initial value
const PI; // Missing Initializer in const declarationCopy the code
- Constants cannot be reassigned
- Has block-level scope
- It is not preparsed
- Constants are also independent and are not pushed into the Window object when defined
Deconstruction assignment
An array of deconstruction
Variables correspond to values one to one
let arr = [5, 9, 10]; let [a, b, c] = arr; console.log(a, b, c); // Output 5, 9, 10Copy the code
More variables, less values
let arr = [5, 9, 10]; let [a, b, c, d] = arr; console.log(a, b, c, d); // Output 5 9 10 undefinedCopy the code
Fewer variables, more values
let arr = [5, 9, 10, 8, 3, 2]; let [a, b] = arr; console.log(a, b); / / 5 and 9Copy the code
On demand the value
let arr = [5, 9, 10, 8, 3, 2]; let [, , a, , b] = arr; Console. log(a, b); / / 10, 3Copy the code
The residual value
let arr = [5, 9, 10, 8, 3, 2]; let [a, b, ...c] = arr; / /... C receives the remaining values, and the resulting C is an array console.log(a, b, c); // a = 5, // b = 9, // c = [10, 8, 3, 2]Copy the code
Complex situations can be deconstructed as long as they conform to the pattern
let arr = ['lilei', 18, ['180cm', '70kg']];
let [, , [a, b]] = arr;
console.log(a, b); // 180cm 70kg
Copy the code
Object to deconstruct
By default, variable names are required to be the same as attribute names
let { foo, bar } = {foo: 'zhangmeimei', bar: 'lilei'};
console.log(foo, bar); // zhangmeimei, lilei
let {a, c} = {a: 'hello', b: 'world'};
console.log(a, c); // hello, undefined
Copy the code
You can change the name of a variable by:
let {a, b:c} = {a: 'hello', b: 'world'};
console.log(a, c); // hello, world
Copy the code
The value is obtained by having the same variable name as the property name, not necessarily one to one
let {b} = {a: 'hello', b: 'world'}; console.log(b); // in this case, the variable a is not defined, so using a will report an errorCopy the code
The residual value
Let obj = {name:'zs', age:20, gender:' male '}; let {name, ... a} = obj; console.log(name, a); / / results: / / name = zs / / a = {age: 20, gender: "male"};Copy the code
Complicated conditions
Let obj = {name: 'zhangsan', age: 22, dog: {name: 'zhangsan', age: 3}}; let obj = {name: 'zhangsan', age: 22, dog: {name: 'zhangsan', age: 3}}; let {dog: {name, age}} = obj; console.log(name, age); / / teddy. 3Copy the code