Home > Web Front-end > JS Tutorial > body text

JavaScript learning: using const to declare constants

WBOY
Release: 2022-08-09 16:47:24
forward
2432 people have browsed it

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

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

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

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

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

// 正确写法
const PI = 3.14;
Copy after login

Initialization The final value cannot be modified

const PI = 3.14;
PI = PI + 1; // 报错
Copy after login

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

But you cannot reassign the value to the object

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

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

Similarly, constant arrays cannot be reassigned:

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

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!

Related labels:
source:csdn.net
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