Home  >  Article  >  Web Front-end  >  JavaScript learning: using const to declare constants

JavaScript learning: using const to declare constants

WBOY
WBOYforward
2022-08-09 16:47:242400browse

This article brings you relevant knowledge about javascript, which mainly introduces the related issues about using const to declare constants. const is used to declare one or more constants, which must be declared. Initialization, and the value cannot be modified after initialization. Let’s take a look at it. I hope it will be helpful to everyone.

JavaScript learning: using const to declare constants

[Related recommendations: javascript video tutorial, web front-end

const is used to declare a or Multiple constants must be initialized when declared, and the values ​​cannot be modified after initialization.

const declares constants

Const defined constants are similar to variables defined using let:

  • Both are block-level scopes
  • Both It cannot have the same name as other variables or functions in its scope.

There are two differences between the two:

  • The constant declared by const must be initialized, and Variables declared by let do not use
  • const. The value of a defined constant cannot be modified by reassignment, nor can it be declared again. The values ​​of variables defined by let can be modified.

Block-level scope

Const-defined constants also have block-level scope

var a = 10;
const x = 'world';
if (a > 0){
    const x = 'hello';
    console.log(x);   // 这里输出 x 为 hello
}
console.log(x);  // 这里输出 x 为 world

Cannot have the same name as other variables or functions in its scope

{
    var x = 'world';
    const x = 'hello';  // 报错
}

Initialization

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

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

The following is the correct way to write, assign the value at the same time of declaration

// 正确写法
const PI = 3.14;

Initialization The final value cannot be modified

const PI = 3.14;
PI = PI + 1; // 报错

Not a real constant

String and numeric types defined using const are immutable. When an object or array is defined, the content inside can be modified. of.

const Define the object to modify the properties

const Define the object to modify the properties

const person = {
    name: "yoyo",
    age: 20,
};
person.name = 'hello';
person.age = 30;
console.log(person.name);   // hello
console.log(person.age);     // age

But you cannot reassign the value to the object

const person = {
    name: "yoyo",
    age: 20,
};
person = {name: 'xx', age: 23};  // 报错

const Defining an array to modify members

const Defining an array can modify the value of a member

const a = ['hello', 'world'];

// 修改元素
a[0] = "yoyo";
console.log(a);  //   ['yoyo', 'world']
a.shift('12');
console.log(a);  //   ['world']
a.unshift('xx');
console.log(a);  //   ['xx', 'world']
a.push('yy');
console.log(a);  //   ['xx', 'world', 'yy']

Similarly, constant arrays cannot be reassigned:

const a = ['hello', 'world'];
a = ['x', 'y']; // 报错

Summary: Constants are values ​​(memory addresses) cannot The amount of change, const definition often requires an initial value.

【Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of JavaScript learning: using const to declare constants. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete