Get the App
SLTechnology News&Howtos  ›  Development  › 

What does const mean in javascript

Shulou Source: shulou.com Published: 2022-06-01 02:16:00 10月05日 Update

This article will explain in detail what const means in javascript. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

In JavaScript, const means "immutable" and is a keyword used to declare one or more constants. The declaration must be initialized, and the value can no longer be modified after initialization. If you change the value of a constant, it will cause a type error. The syntax is "const constant name = constant value;".

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

What is the meaning of const in javascript

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

Variables declared by let are valid only within the block of code where the let command is located.

Const declares a read-only constant, and once declared, the value of the constant cannot be changed.

Used to declare one or more constants, which must be initialized, and the value cannot be modified after initialization

Const defines constants similar to variables defined using let:

Both are block-level scope

Cannot have the same name as any other variable or function in its scope

There are two other differences between the two:

Constants declared by const must be initialized, while variables declared by let do not

The value of the const definition constant cannot be modified by re-assignment, nor can it be declared again. The value of the variable defined by let can be modified.

Examples are as follows:

123 JavaScript const

Const is used to declare one or more constants, which must be initialized at the time of declaration, and the value cannot be modified after initialization.

Try {const PI = 3.141592653589793; PI = 3.14;} catch (err) {document.getElementById ("demo") [xss_clean] = err;}

Output result:

This is the end of the article on "what does const in javascript mean?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Constant variable multiple article meaning function key keyword meaning location more good practical effective similar same important no two code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Apple Xiaomi MariaDB OPPO Reno