简体   繁体   English

什么时候“未定义”等于JavaScript中的“null”?

[英]When is “undefined” equal to “null” in JavaScript?

I've been watching some videos from Tut+ about Js.They said that sometimes "undefined" is equal to "null". 我一直在看Tut +关于Js的一些视频。他们说有时候“undefined”等于“null”。 So, when does this happen ? 那么,这是什么时候发生的?

undefined == null
// => true

undefined === null
// => false

== tests for equality, === tests for identity (or strict equality). ==测试相等性, ===测试身份(或严格相等)。 If in doubt, use === . 如有疑问,请使用===

Just to add on , This question is somehow answered already, check here 只是补充一下,这个问题已经以某种方式回答了, 请点击此处

You can just check if the variable has a truthy value or not. 您可以检查变量是否具有真值。 That means 这意味着

 if( value ) {

 }

will evaluate to true if value is not: 如果值不是,则评估为真:

null

undefined

NaN

empty string ("")

0

false

this is because of the poor typing in JS 这是因为JS输入不好

null === undefined // false 
null == undefined  // true

great!!! 大!!!

good practice is not to use null at all in js. 好的做法是不要在js中使用null。 You cannot not get rid of undefined because it is built in. if you access an undefined variable its === undefinded, but not equals null, right? 你不能没有摆脱不确定,因为它是内置的。如果你访问一个未定义的变量及其=== undefinded,但不等于空,对不对? So to not get confused, just stop using it. 所以不要混淆,只是停止使用它。 define it as undefined, but not as null ;) 将其定义为undefined,但不是null;)

so don't use null 所以不要使用null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM