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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:52:28  来源:igfitidea点击:

When declaring a variable in javascript, is the default value null?

javascript

提问by thisissami

If I have a declaration as follows:

如果我有如下声明:

var j;

does j==nulluntil 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 !jcondition, it will work with both the values (i.e. undefinedor null)

不,它的默认值是undefined
但如果要使用!j条件,它将使用两个值(即undefinednull

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”的价值观和有时还会带来意想不到的规则来转换数值,加上看中===操作者在同一时间比较值和类型。