In JavaScript, a 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. The initialization value cannot be re-modified.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
The value of variables declared using var and let can change during the running of the script code. If you want the value of a variable to remain unchanged throughout the running of the script code, you need to use const to declare it. Once defined, it cannot be modified (that is, what is defined using the const keyword is a constant).
The declaration format is as follows:
const 变量名 = 值;
The so-called 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. Once defined, it cannot be modified.
Constants, like 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 during runtime
const a = 1 //当常量a被创建时,再次给a赋值时,a仍为1 console.log(a); a = 10; console.log(a) // 报错
Classification of constants
1. Integer constants
Integer constants are actually positive numbers. Any integer written in JavaScript is an integer. Constants
2. Real constants
Real constants are decimals
3. String constants
String constants actually use single quotes or double quotes. The contents enclosed in quotation marks are called string constants
4, Boolean constants
Boolean constants are actually true or false, which are expressed by true and false in JavaScript
In JavaScript, Boolean constants have only two values, true (true) or false (false)
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What do constants mean in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!