Home >Web Front-end >Front-end Q&A >What are constants in javascript
In JavaScript, variables that cannot be modified once defined are called constants. Constants represent some fixed data and are values that cannot be modified. The keyword for defining a constant is const. It must be initialized when defining, and the value cannot be modified after initialization. The syntax is "const variable name = value;".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In JavaScript, variables that cannot be modified once defined are called constants.
Constants represent some fixed data and cannot be modified; constants can only be read and cannot be modified.
The keyword of constant is const:
const can define one or more constants, which must be initialized when defining, 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. .
const NUM = 666; // NUM = 888; //尝试修改NUM这个常量的取值,会发现报错了,常量一旦定义就不能再去改变值了 console.log(NUM);
Extended knowledge: Classification of constants in JavaScript
Integer constants
Integer constants are actually positive numbers. Any integer written in JavaScript is an integer constant.
1 / 666 / 99
Real constants
Real constants are actually decimals. Any decimal written in JavaScript is a real constant
3.14 / 6.66
String constant
String constant is actually using single quotes or double quotes The content enclosed in quotation marks is called a string constant
'a'
'abc'
“1”
“BNTang”
Note: No matter how many are enclosed in single quotes or double quotes characters, in JavaScript they are all string constants
##Boolean constants
Custom constants
Newly added in ES6const 常量名称 = 常量取值;[Related recommendations:
The above is the detailed content of What are constants in javascript. For more information, please follow other related articles on the PHP Chinese website!