Javascript 在javascript中声明变量时,默认值是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10560362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When declaring a variable in javascript, is the default value null?
提问by thisissami
If I have a declaration as follows:
如果我有如下声明:
var j;
does j==null
until I set it equal to something?
确实j==null
,直到我将其设置为东西吗?
回答by Danilo Valente
No, it has the default value of undefined
But if want to use the !j
condition, it will work with both the values (i.e. undefinedor null)
不,它的默认值是undefined
但如果要使用!j
条件,它将使用两个值(即undefined或null)
Note that (j==null)
is true
, but (j===null)
is false
... JavaScript have "falsy" values and sometimes unexpected rules to convert values, plus fancy ===
operator to compare value and type at the same time.
请注意,(j==null)
是true
,但是(j===null)
是false
... JavaScript的有“falsy”的价值观和有时还会带来意想不到的规则来转换数值,加上看中===
操作者在同一时间比较值和类型。