What is the usage of const in es6

WBOY
Release: 2022-05-06 17:38:45
Original
1709 people have browsed it

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.

What is the usage of const in es6

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

What is the usage of const in es6

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
Copy after login

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 = &#39;tian&#39;;
    // 会报错: Uncaught TypeError: Assignment to constant variable. 
    // name = &#39;kim&#39;;
  </script>
</body>
</html>
Copy after login

[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!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template