Home Web Front-end JS Tutorial What is the difference between let and const

What is the difference between let and const

Dec 24, 2020 pm 12:02 PM
const let

Difference: Variables declared by let can be changed, and both value and type can be changed; while constants declared by const cannot be changed, which means that once const is declared, it must be initialized immediately and cannot be assigned a value later.

What is the difference between let and const

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Related recommendations: "javascript video tutorial"

The difference between let and const:

① The variables declared by let can be changed, and both the value and type can be changed; the constants declared by const cannot be changed, which means that once const is declared, it must be initialized immediately and cannot be assigned a value later.

const i ; // 报错,一旦声明,就必须立即初始化
const j = 5;
j = 10; // 报错,常量不可以改变
Copy after login

②For composite type variables such as arrays and objects, the variable name does not point to the data, but to the address where the data is located. const only guarantees that the address pointed to by the variable name remains unchanged, but does not guarantee that the data at that address remains unchanged, so you must be very careful when declaring a composite type variable as a constant.

const arr = [];
// 报错,[1,2,3]与[]不是同一个地址
arr = [1,2,3];
const arr = [];
// 不报错,变量名arr指向的地址不变,只是数据改变
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
console.log(arr.length); // 输出:3
Copy after login

If you want the data of the defined object or array not to change, you can use object.freeze(arr) to freeze. Freezing means that new attributes cannot be added to this object or array, the values ​​of existing attributes cannot be modified, and existing attributes cannot be deleted.

const arr = [];
Object.freeze(arr);
// 不报错,但数据改变无效
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
console.log(arr.length); // 输出:0
Copy after login

The similarities between let and const:

① It is only valid within the block-level scope where it is declared.

② No promotion, and there is a temporary dead zone, which can only be used after the declared position.

③ Cannot be repeated.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of What is the difference between let and const. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Deep understanding of const in C language Deep understanding of const in C language Feb 18, 2024 pm 12:56 PM

Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

Let's talk about the differences between var, let and const (code example) Let's talk about the differences between var, let and const (code example) Jan 06, 2023 pm 04:25 PM

This article brings you relevant knowledge about JavaScript. It mainly introduces the differences between var, let and const, as well as the relationship between ECMAScript and JavaScript. Interested friends can take a look at it. I hope Helpful to everyone.

How to use const in c language How to use const in c language Sep 20, 2023 pm 01:34 PM

const is a keyword that can be used to declare constants, const modifiers in function parameters, const modified function return values, and const modified pointers. Detailed introduction: 1. Declare constants. The const keyword can be used to declare constants. The value of the constant cannot be modified during the running of the program. The constant can be a basic data type, such as integer, floating point number, character, etc., or a custom data type; 2. The const modifier in the function parameters. The const keyword can be used in the parameters of the function, indicating that the parameter cannot be modified inside the function, etc.

18 Ways to Fix Audio Service Not Responding Issue on Windows 11 18 Ways to Fix Audio Service Not Responding Issue on Windows 11 Jun 05, 2023 pm 10:23 PM

Audio output and input require specific drivers and services to work as expected on Windows 11. These sometimes end up running into errors in the background, causing audio issues like no audio output, missing audio devices, distorted audio, etc. How to Fix Audio Service Not Responding on Windows 11 We recommend you to start with the fixes mentioned below and work your way through the list until you manage to resolve your issue. The audio service may become unresponsive for a number of reasons on Windows 11. This list will help you verify and fix most issues that prevent audio services from responding on Windows 11. Please follow the relevant sections below to help you through the process. Method 1: Restart the audio service. You may encounter

What are the correct uses of the const keyword in C++ functions? What are the correct uses of the const keyword in C++ functions? Apr 11, 2024 pm 02:36 PM

Correct usage of the const keyword in C++: Using const to modify a function means that the function will not modify the parameters or class members passed in. Using const to declare a function pointer means that the pointer points to a constant function.

C++ syntax error: const objects must be initialized when defined, how to deal with it? C++ syntax error: const objects must be initialized when defined, how to deal with it? Aug 22, 2023 am 09:13 AM

For C++ programmers, syntax errors are one of the most common problems. One of the common mistakes is that const objects must be initialized at definition time. If you encounter this situation, how should you deal with it? First, we need to understand what a const object is. The const keyword is a special type qualifier in C++ that specifies that the value of a variable cannot be changed during the execution of the program. Such variables are called "constants". If you define a const object without initializing it, you will encounter the above error. This is

The differences and characteristics of let, var and const: What do they mean? The differences and characteristics of let, var and const: What do they mean? Feb 23, 2024 pm 12:36 PM

The differences and characteristics of let, var and const: What do they mean? In JavaScript, let, var, and const are keywords used to declare variables. Each of them has different differences and characteristics. let: The let keyword was introduced in ES6, which allows us to declare a block-scoped variable. Block-level scope means that the variable is only visible in the block where it is declared and will not be promoted to the function scope. Example code: functionexampleFunctio

C++ error: Cannot convert const object to non-const object, how to solve it? C++ error: Cannot convert const object to non-const object, how to solve it? Aug 22, 2023 am 08:33 AM

As a strongly typed language, C++ needs to consider many details when performing type conversion. A common problem is that const objects cannot be converted into non-const objects. This problem is more common when pointers and references are involved. Next, we will detail the causes and solutions to this problem. The cause of the problem is that the const keyword in C++ is used to define constants. Once a constant is defined, it cannot be modified. When we convert a const object to a non-const object, we are actually trying to modify a

See all articles