I've been watching some videos from Tut+ about Js.They said that sometimes "undefined" is equal to "null". So, when does this happen ?
Asked
Active
Viewed 329 times
4
-
This is a pretty broad question. Do you have a specific issue you'd like help with? – Brody Sep 09 '15 at 02:33
3 Answers
7
undefined == null
// => true
undefined === null
// => false
== tests for equality, === tests for identity (or strict equality). If in doubt, use ===
.

Amadan
- 191,408
- 23
- 240
- 301
-
2That's a very misleading summary of `==` and `===`. They both test for equality (or identity for objects) but the former uses a complex algorithm of type coercion when testing equality of differing types. – Sep 09 '15 at 02:42
-
@squint: Indeed, as summaries generally are. Which is why I linked to the documentation, which is much more specific and exhaustive. – Amadan Sep 09 '15 at 03:13
-
Huh? Summaries don't need to be misleading at all. Your description doesn't reflect the reality of how those operators work at all. It's just bad information. – Sep 09 '15 at 04:44
-
@squint: This is how they are named in MDN. If you have an issue with that, take it up with Mozilla's documentation team. (ECMA says ["abstract equality"](http://www.ecma-international.org/ecma-262/6.0/#sec-abstract-equality-comparison) and ["strict equality"](http://www.ecma-international.org/ecma-262/6.0/#sec-strict-equality-comparison), not too different). – Amadan Sep 09 '15 at 04:46
-
ECMA refers to the *Abstract Equality Comparison Algorithm* or the *Strict Equality Comparison Algorithm*. That's very different from what you wrote. There is no *"Mozilla documentation team"* for MDN. It's a wiki. – Sep 09 '15 at 04:48
-
@squint: Great. Go edit it then. When you do, I will update the answer. – Amadan Sep 09 '15 at 04:51
-
1
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

Community
- 1
- 1

Milad Rezazadeh
- 1,473
- 12
- 34
0
this is because of the poor typing in JS
null === undefined // false
null == undefined // true
great!!!
good practice is not to use null at all in js. 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? So to not get confused, just stop using it. define it as undefined, but not as null ;)
so don't use null

Pritam Banerjee
- 17,953
- 10
- 93
- 108

webdeb
- 12,993
- 5
- 28
- 44
-
3It isn't poor typing. Having `null` and `undefined` coerce to equal values when using `==` is very useful. Telling people not to use `null` is just bad advice. It's part of the language and is useful. – Sep 09 '15 at 02:46
-
@squint it's not my words, you know who douglas crockford is, he is the guy who is saying it. And actually he is the guy who is also involved in the design process of ES.. – webdeb Sep 09 '15 at 02:49
-
1Doug Crockford's *opinions* mean very little to me. He's very influential among beginners because he speaks authoritatively but his advice is often really, really poor. – Sep 09 '15 at 02:51
-
null is used throughout the DOM to indicate unset property values as opposed to made-up properties values, but Doug's probably mostly right about not needing to set anything to null manually. since it appears in JSON as well, if(x!=null) can be a handy shortcut to the two type explicit checks you probably care about. – dandavis Sep 09 '15 at 03:01
-
1@webdeb there is a big difference between `undefined` and `null`. `undefined` is when you're accessing something that doesn't exists. But `null` is when you're accessing something that is `null`. One should be implicit while the other is an explicit value. You shouldn't set explicitly `undefined` to anything, instead you should always set it to `null`. – Loïc Faure-Lacroix Jan 27 '16 at 08:20