Home >Web Front-end >JS Tutorial >Introduction to the differences between let, var and const in JavaScript and their application scenarios

Introduction to the differences between let, var and const in JavaScript and their application scenarios

不言
不言Original
2018-12-12 15:59:384786browse

In Introduction to the differences between let, var and const in Introduction to the differences between let, var and const in Introduction to the differences between let, var and const in JavaScript and their application scenarios and their application scenarios and their application scenarios, it is recommended to declare variable names (hereinafter collectively referred to as "variables") when using variables and constants. var, let and const are keywords used when declaring variables in Introduction to the differences between let, var and const in Introduction to the differences between let, var and const in Introduction to the differences between let, var and const in JavaScript and their application scenarios and their application scenarios and their application scenarios, where let and const are keywords for the new declaration method adopted by ECMAScript 2015.

Introduction to the differences between let, var and const in Introduction to the differences between let, var and const in Introduction to the differences between let, var and const in JavaScript and their application scenarios and their application scenarios and their application scenarios

ECMAScript 2015 (ekma script) is the standard specification for Introduction to the differences between let, var and const in Introduction to the differences between let, var and const in Introduction to the differences between let, var and const in JavaScript and their application scenarios and their application scenarios and their application scenarios. Standardized by international organizations, it is widely adopted in modern browsers such as Google Chrome and Internet Explorer 11 and above.

Using let and const, you can prevent the wrong value from being overwritten, or a variable with the same name being declared incorrectly.

Let’s take a detailed look at the differences and usage of var, let and const

var

In var, you can re-declare and redistribute.

var techacademy = '初始值ok';
techacademy = '重新分配ok';
var techacademy = '重新声明ok';

let

Re-declaration is prohibited in let

let techacademy = '初始值ok';
techacademy = '重新分配ok';
let techacademy = '重新声明no';

const

in const , redeclaration and reassignment are prohibited.

const techacademy = '初始值ok';
techacademy = '重新分配no';
const techacademy = '重新声明no';

Let’s use examples to explain let, var and const in detail

The writing methods of let, var and const

The basic procedure is as follows. After each keyword, write the variable name and initial value. The initial values ​​of var and let can be omitted.

var hoge1 = 'hoge1';
var hoge2;  // 省略初始值的写法
let fuga1 = 'fuga1';
let fuga2;  // 省略初始值的写法
const piyo = 'piyo'; // const的初始值不能省略

The usage scope of let, var and const

The usage scope of var, let and const are different.

For example, if a var is declared outside an if statement, it can also be used in an if statement.

let and const cannot be used if they exceed the scope.

if (1) {
var x = 'ok';
let y = 'no';
const z = 'no';
}
console.log(x); //可以使用
console.log(y); //范围外,错误
console.log(z); //范围外,错误

Let’s look at specific examples

Variable declaration basically uses const. The error occurs when reassigning const. Therefore, by declaring a variable whose value you don't want to change using const, you prevent the value from being accidentally changed.

const piyo = 'piyo';
piyo = 'hogehoge';  // 发生错误

The operation effect is as follows

Introduction to the differences between let, var and const in Introduction to the differences between let, var and const in Introduction to the differences between let, var and const in JavaScript and their application scenarios and their application scenarios and their application scenarios

If you need to redistribute, you can use let. Because let can set the valid scope of a variable locally compared to var, it prevents mistaken declaration of variables with the same name.

{
  let fuga = 'fuga';  // 仅在{ ... }中有效 
  fuga = 'fugafuga';  // 重新分配,没有错误
  console.log(fuga);
}
console.log(fuga);  // 发生错误

The operation effect is as follows

Introduction to the differences between let, var and const in Introduction to the differences between let, var and const in Introduction to the differences between let, var and const in JavaScript and their application scenarios and their application scenarios and their application scenarios

In most cases, it is recommended to use const and let. Use var only when absolutely necessary; this prevents incorrect use of variables and therefore makes programming and debugging easier.

The above is the detailed content of Introduction to the differences between let, var and const in JavaScript and their application scenarios. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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