Home  >  Article  >  Web Front-end  >  What is const in javascript

What is const in javascript

青灯夜游
青灯夜游Original
2021-06-09 11:53:1120549browse

const is a keyword built into JavaScript. Const is used to declare one or more constants. Read-only constants can be declared. They must be initialized when declaring. Once declared, the value of the constant cannot be changed. A constant cannot have the same name as another variable or function in its scope.

What is const in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

ES2015 (ES6) adds two important JavaScript keywords: let and const.

The variables declared by let are only valid within the code block where the let command is located.

const is used to declare one or more constants. They must be initialized when declaring, and the value cannot be modified after initialization:

const PI = 3.141592653589793;
PI = 3.14;      // 报错
PI = PI + 10;   // 报错

Const defined constants are similar to variables defined using let:

  • Both are block-level scopes

  • Neither can have the same name as other variables or functions in its scope

There are two differences between the two:

  • The constants declared by const must be initialized, while the variables declared by let do not need to be initialized

  • const The value of a defined constant cannot be modified through reassignment, nor can it be declared again. The values ​​of variables defined by let can be modified.

var x = 10;
// 这里输出 x 为 10
{ 
    const x = 2;
    // 这里输出 x 为 2
}
// 这里输出 x 为 10

const The declared constant must be initialized:

// 错误写法
const PI;
PI = 3.14159265359;

// 正确写法
const PI = 3.14159265359;

is not a real constant

The essence of const: defined by const A variable is not a constant and is not immutable. It defines a constant that refers to a value. Objects or arrays defined using const are actually mutable. The following code will not report an error:

// 创建常量对象
const car = {type:"Fiat", model:"500", color:"white"};
 
// 修改属性:
car.color = "red";
 
// 添加属性
car.owner = "Johnson";

But we cannot reassign the constant object:

const car = {type:"Fiat", model:"500", color:"white"};
car = {type:"Volvo", model:"EX60", color:"red"};    // 错误

The following example modifies the constant array:

// 创建常量数组
const cars = ["Saab", "Volvo", "BMW"];
 
// 修改元素
cars[0] = "Toyota";
 
// 添加元素
cars.push("Audi");

But we cannot reassign the constant array Reassignment:

const cars = ["Saab", "Volvo", "BMW"];
cars = ["Toyota", "Volvo", "Audi"];    // 错误

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of What is const in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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