Home  >  Article  >  Web Front-end  >  Comparison of let and const usage in ES6

Comparison of let and const usage in ES6

巴扎黑
巴扎黑Original
2017-07-23 16:21:141517browse

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>

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;

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

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

<br/>

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.

##  }
  alert(a)//Error report

Once a variable is declared, it must be initialized immediately and cannot be left until Assign value later.

  //只声明不赋值就会报错
  const foo; //报错

const has no variable promotion like let, and it cannot be declared repeatedly.

 <br/>

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!

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
Previous article:javascript--Date objectNext article:javascript--Date object