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

What is used to define constants in javascript

青灯夜游
Release: 2021-11-03 18:17:02
Original
5302 people have browsed it

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

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

[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!

Related labels:
source:php.cn
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