What are constants in javascript

青灯夜游
Release: 2021-12-07 17:04:32
Original
3157 people have browsed it

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;".

What are constants in javascript

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;
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. .

const NUM = 666;
// NUM = 888; //尝试修改NUM这个常量的取值,会发现报错了,常量一旦定义就不能再去改变值了
console.log(NUM);
Copy after login

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

Real constants

Real constants are actually decimals. Any decimal written in JavaScript is a real constant

3.14 / 6.66
Copy after login

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

  • Boolean constants are actually true or false , expressed through true and false in JavaScript

  • In JavaScript, Boolean constants have only two values, true (true) or false (false)

Custom constants

Newly added in ES6

const 常量名称 = 常量取值;
Copy after login
[Related recommendations:

javascript learning tutorial]

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!

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