constlet 宣告

var 不同,const / let 與詞法範圍而非功能範圍繫結。

{
  var x = 1 // will escape the scope
  let y = 2 // bound to lexical scope
  const z = 3 // bound to lexical scope, constant
}

console.log(x) // 1
console.log(y) // ReferenceError: y is not defined
console.log(z) // ReferenceError: z is not defined

在 RunKit 中執行