Home  >  Article  >  Web Front-end  >  What is used to define constants in javascript

What is used to define constants in javascript

青灯夜游
青灯夜游Original
2021-11-03 18:17:025278browse

The const keyword can be used to define constants in JavaScript. This keyword can define one or more constants. They must be initialized when defining, and the value cannot be modified after initialization; the syntax is "const variable name=value;" Or "const variable name 1 = value 1, variable name 2 = value 3,..., variable name n = value n;".

What is used to define constants in javascript

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

Constant is a quantity that cannot be changed. It is usually used to store data that does not change and is not expected to change. The value of a constant remains unchanged throughout the entire running process of the script code and cannot be re-initialized. worth it.

In javascript, you can use the const keyword to define constants.

const can define one or more constants, which must be initialized when defined, and the value cannot be modified after initialization.

Syntax:

const 变量名=值;
const 变量名1=值1,变量名2=值3,...,变量名n=值n;

Note: Constants and variables are containers used to store data, but the value of the constant cannot be changed during the running of the program, otherwise an error will be reported at runtime. .

Example:

<script>
try {
    const PI = 3.141592653589793;
    PI = 3.14;
}
catch (err) {
    document.getElementById("demo").innerHTML = err;
}
</script>

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What is used to define constants 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