JavaScript几种声明变量的方式以及变量提升

259次阅读
没有评论

在es6之前,生命变量都是使用关键词var,es6之后推出了let const,用var声明变量会导致变量提升,而let、const是块级作用域旧就不会,还有一种方式就是不用任何关键字,比方直接 a = 1,那么这种就是全局声明。

变量提升,就是在执行代码之前,变量的声明会被提升到其作用域的顶部。

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

输出的答案是:

undefined
1

如果使用 const 或者 let ,那么就会直接报错提示 a is not defined

console.log(a)
const a = 1
console.log(a)
正文完
 0
wujingquan
版权声明:本站原创文章,由 wujingquan 于2023-08-27发表,共计269字。
转载说明:Unless otherwise specified, all articles are published by cc-4.0 protocol. Please indicate the source of reprint.
评论(没有评论)