Home > Web Front-end > Front-end Q&A > What do constants mean in JavaScript?

What do constants mean in JavaScript?

青灯夜游
Release: 2021-10-19 13:58:11
Original
3124 people have browsed it

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.

What do constants mean in JavaScript?

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 变量名 = 值;
Copy after login

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) // 报错
Copy after login

What do constants mean in JavaScript?

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!

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