Home > Web Front-end > JS Tutorial > body text

Comparison of let and const usage in ES6

巴扎黑
Release: 2017-07-23 16:21:14
Original
1531 people have browsed it

let and const

let

are used to declare variables, but the declared variables are only where the let command is located Valid within a code block

<span style="font-size: 18px"><code>  {<br/>    let a=12<br/>    alert(a)//12<br/>  }<br/>  alert(a)//报错 找不到</code></span>
Copy after login

let does not cause variable promotion like var, so it must be declared first and then used.

  console.log(foo); //undefined
  console.log(bar); //报错
  var foo = 2;
  let bar = 3;
Copy after login

let does not allow repeated declaration of the same variable in the same scope.

##   let a=12;<br/> let a=5;//Error report<br/> console.log(a)

<br/>
Copy after login

const

#constYou can also create block-scope variables, also only in the block-level scope where they are declared. Medium and effective. But its value is fixed, cannot be changed, and is read-only.

##  }<br/>   alert(a)//Error report<br/> <br/>Once a variable is declared, it must be initialized immediately and cannot be left until Assign value later.

  //只声明不赋值就会报错
  const foo; //报错
Copy after login
<br/>const has no variable promotion like let, and it cannot be declared repeatedly.

 <br/>
Copy after login

The above is the detailed content of Comparison of let and const usage in ES6. 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