In es6, const is used to declare a read-only constant. The syntax is "const constant name = constant value;"; once a constant is declared, the value of the constant cannot be changed, and only in the block where it is declared. It is valid within the level scope. Constants declared by the const command are not promoted and can only be used after the declared position.
The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
1. Const declares a read-only constant. Once declared, the value of a constant cannot be changed.
2. The scope of const is the same as that of the let command: it is only valid within the block-level scope where the declaration is located.
3. The constants declared by the const command are not promoted and can only be used after the declared position.
4. In ES6: global variables declared by the var command and function command are still attributes of the top-level object; on the other hand, global variables declared by the let command, const command, and class command do not belong to the top-level object. properties. In other words, starting from ES6, global variables will gradually be decoupled from the properties of the top-level object.
let b = 1; //控制台输出undefined window.b
In JavaScript, const means "constant" and is a keyword used to declare one or more constants. They must be initialized when declaring, and the value cannot be modified after initialization. If Changing the value of a constant will cause a type error, and the syntax is "const constant name = constant value;".
Once a const-modified identifier is assigned a value, it cannot be modified
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> // 1.注意一: 一旦给const修饰的标识符被赋值之后,不能修改 // const name = 'tian'; // 会报错: Uncaught TypeError: Assignment to constant variable. // name = 'kim'; </script> </body> </html>
[Related recommendations: javascript video tutorial, web front-end]
The above is the detailed content of What is the usage of const in es6. For more information, please follow other related articles on the PHP Chinese website!