Home Web Front-end JS Tutorial The difference between const, var and let in js

The difference between const, var and let in js

Oct 12, 2016 pm 01:07 PM
var

The main content is: the differences between the three ways of defining variables in js: const, var, and let.

1. Variables defined by const cannot be modified and must be initialized.

const b = 2; // Correct

// const b; // Error, must be initialized

console.log('Const definition b outside function: ' + b); // There is an output value

// b = 5;

// console.log('Modify const definition b outside function: ' + b); //Cannot output

2. Variables defined by var can be modified. If not initialized, undefined will be output and no error will be reported. .

var a = 1;
// var a;//不会报错
console.log('函数外var定义a:' + a);//可以输出a=1
function change(){
a = 4;
console.log('函数内var定义a:' + a);//可以输出a=4
} 
change();
console.log('函数调用后var定义a为函数内部修改值:' + a);//可以输出a=4
Copy after login

3.let is a block-level scope. After the function is defined using let, it has no impact on the outside of the function.

let c = 3;
console.log('函数外let定义c:' + c);//输出c=3
function change(){
let c = 6;
console.log('函数内let定义c:' + c);//输出c=6
} 
change();
console.log('函数调用后let定义c不受函数内部定义影响:' + c);//输出c=3
Copy after login


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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1654
14
PHP Tutorial
1252
29
C# Tutorial
1225
24
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.

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

The role and examples of var keyword in PHP The role and examples of var keyword in PHP Jun 28, 2023 pm 08:58 PM

The role and examples of var keyword in PHP In PHP, the var keyword is used to declare a variable. In previous PHP versions, using the var keyword was the idiomatic way to declare member variables, but its use is no longer recommended. However, in some cases, the var keyword is still used. The var keyword is mainly used to declare a local variable, and the variable will automatically be marked as local scope. This means that the variable is only visible within the current block of code and cannot be accessed in other functions or blocks of code. Use var

Solution to the error AttributeError(\'{0!r} object has no attribute {1!r}\'.format(type(self).__name__, k)) Solution to the error AttributeError(\'{0!r} object has no attribute {1!r}\'.format(type(self).__name__, k)) Feb 29, 2024 pm 06:40 PM

The reason for the error message indicates that in the python code, an object (represented by the self variable) is used, but the object does not have an attribute named k. This may be because the object does not have this property defined, or a type error in the code causes the object to not be of the expected type. How to Fix To resolve this error, you may need to do one or more of the following: Check your code for the error and make sure the object referenced by the self variable has a property named k. Check your code for type errors and make sure the object referenced by the self variable is of the expected type. If the attribute is missing, you need to define this attribute in the class and use tryexcept to get this error. If you are sure that k is an attribute that is not defined in the class, please confirm

What does let var const mean? What does let var const mean? Nov 14, 2023 pm 03:00 PM

llet, var, and const represent block scope variables, function scope variables, and constants respectively. Detailed introduction: 1. let, used to declare a variable in a block scope. A variable declared using let cannot be accessed before it is declared. This is the so-called temporary dead zone; 2. var, used to declare the key to a variable. Word, the declared variable is in function scope or global scope and is not restricted by block-level scope; 3. const, used to declare a constant. Once assigned, the variable cannot be reassigned. The value is after declaration. Cannot be modified etc.

Over a thousand Android apps have made their way to Windows 11 today Over a thousand Android apps have made their way to Windows 11 today May 09, 2023 pm 07:01 PM

Windows Subsystem for Android is here When Microsoft announced Windows 11 last year, it said Android apps would run on the new operating system. Unfortunately, it wasn't released in time and ended up heading to Insiders later this year. It will initially only have 50 apps available, curated by Microsoft and Amazon. Now anyone can use it if you meet the hardware requirements and it will offer over a thousand applications. I'm a big extension to the small subset of Insiders I've been able to test, but it's still a small part of the Amazon App Store. I did ask the developers when they would be able to submit the app and run it on Windows

Query the disk space usage of CentOS system Query the disk space usage of CentOS system Feb 20, 2024 am 08:09 AM

Command introduction: Use the df-h command to view the total capacity and used capacity of each file system. Your output shows that /dev/mapper/centos-root takes up 47% of the space, which is 36G. Use the du-h-x–max-depth=1/ command to check the size of each subdirectory under the root directory. You can find out the directories that take up a lot of space based on the output, such as /var, /home, /usr, etc. You can run the command du-h-x--max-depth=1/var to check the size of each subdirectory in the /var directory. Continue this process until you find a file or directory that takes up more space. Recommended articles https://blog.csdn.n

Distinguish the different characteristics of var, let and const Distinguish the different characteristics of var, let and const Feb 19, 2024 pm 05:24 PM

To understand the different characteristics of var, let, and const, you need specific code examples. In JavaScript, there are many ways to declare variables, the most common of which include using the var, let, and const keywords. Although they are both used to declare variables, they have different characteristics regarding scope and mutability. The differences between them are explained below with specific code examples. var keyword Let’s first look at the usage of var keyword. It is the earliest introduced way to declare variables, with global scope and

See all articles