javascript Declarations

,变量名被称为identifiers,三种声明变量的方式:

方式 描述
var local and global variables,默认undefined
let block-scope local variable,默认undefined

constblock-scope local variable,只读常量

注意:
1.不使用var let 或const,而直接赋值,比如x=1,是定义了一个undeclared global variable.不建议这么使用.
2.可以使用undefined来判断变量是否被声明,直接使用变量,来判断变量是否有值

var input;
if (input === undefined) {
  doThis();
} else {
  doThat();
}

3.undefined在布尔判断中 == false,在数值计算中 == NaN
4.null在数值计算中 == 0
5.block-scope

if (true) {
  var x = 5;
}
console.log(x);  // x is 5

if (true) {
  let y = 5;
}
console.log(y);  // ReferenceError: y is not defined

6.全局变量Global variables window.variable

参考:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types

发布了1794 篇原创文章 · 获赞 582 · 访问量 154万+

猜你喜欢

转载自blog.csdn.net/claroja/article/details/104308975