What are the differences between var let const in JavaScript

What are the differences between var let const in JavaScript

1. Repeated declaration

var supports repeated declaration, but let and const do not support repeated declaration.

1.1 var

var a = 1;
var a = 2;
console.log(a);

Output:

2

1.2 let

let b = 3;
let b = 4;
console.log(b);

Output:

Uncaught SyntaxError: Identifier 'b' has already been declared

1.3 const

const c = 5;
const c = 6;
console.log(c);

Output:

Uncaught SyntaxError: Identifier 'c' has already been declared

2. Variable promotion

var supports variable promotion, but only promotes the declaration and not the value. let and const do not support variable promotion.

2.1 var

a=2;
console.log(a);
var a = 1;

Output:

2

2.2 let

a=2;
console.log(a);
let a = 1;

Output:

Uncaught ReferenceError: Cannot access 'a' before initialization at index.html:28

2.3 const

a=2;
console.log(a);
const a = 1;

Output:

Uncaught ReferenceError: Cannot access 'a' before initialization at index.html:28

3. Temporary dead zone

There is no temporary dead zone for var, but there is a temporary dead zone for let and const.
As long as let and const exist in the scope, the variables or constants they declare are automatically "bound" to this area and are no longer affected by the external scope.

3.1 var

var a = 1;
function fun() {
    console.log(a);
    var a = 2;
}
fun();

Output:

undefined

3.2 let

let a = 1;
function fun() {
    console.log(a);
    let a = 2;
}
fun();

Output:

Uncaught ReferenceError: Cannot access 'a' before initialization

3.3 conset

let a = 1;
function fun() {
    console.log(a);
    const a = 2;
}
fun();

Output:

Uncaught ReferenceError: Cannot access 'a' before initialization

4. Properties and methods of the window object

In the global scope, variables declared by var and functions declared by function will automatically become properties and methods of the window object.

var a = 1;
function add() { };
console.log(window.a === a);
console.log(window.add === add);

Output:

true
true

5. Block Scope

var does not have block-level scope, but let and const do.
Use var to define the variable i in the for loop:

for (var i = 0; i < 3; i++) {
    // console.log(i);
}
console.log(i);

Output:

3

Use let to define the variable i in the for loop:

for (let i = 0; i < 3; i++) {
    // console.log(i);
}
console.log(i);

Output:

Uncaught ReferenceError: i is not defined

This concludes this article about the differences between the usage of var, let, and const in JavaScript. For more information on the usage of JavaScript var, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the difference between var, let and const in JavaScript
  • Detailed explanation of the differences between var, let and const in JavaScript es6
  • Detailed explanation of the differences and usage of var in JavaScript and let and const in ES6 specifications
  • Understand the difference between let, var and const keywords in JavaScript
  • The difference between let, const and var in JavaScript ES6 syntax

<<:  Implementation of automatic completion of Docker commands

>>:  HTML table markup tutorial (14): table header

Recommend

What you need to understand about MySQL locks

1. Introduction MySQL locks can be divided into g...

Definition and function of zoom:1 attribute in CSS

Today I was asked what the zoom attribute in CSS ...

HTML thead tag definition and usage detailed introduction

Copy code The code is as follows: <thead> &...

Detailed explanation of the watch listener example in vue3.0

Table of contents Preface The difference between ...

HTML Tutorial: title attribute and alt attribute

XHTML is the basis of CSS layout. jb51.net has al...

The pitfalls of deploying Angular projects in Nginx

Searching online for methods to deploy Angular pr...

Use JavaScript to create page effects

11. Use JavaScript to create page effects 11.1 DO...

MySQL stored procedure in, out and inout parameter examples and summary

Stored Procedures 1. Create a stored procedure an...

What to do if you forget your password in MySQL 5.7.17

1. Add skip-grant-tables to the my.ini file and r...

Detailed explanation of how to upgrade software package versions under Linux

In the Linux environment, you want to check wheth...

MySQL lock control concurrency method

Table of contents Preface 1. Optimistic Locking A...

Detailed analysis of each stage of nginx's http request processing

When writing the HTTP module of nginx, it is nece...

How to use CSS style to vertically center the font in the table

The method of using CSS style to vertically cente...