4

I've been watching some videos from Tut+ about Js.They said that sometimes "undefined" is equal to "null". So, when does this happen ?

1
  • This is a pretty broad question. Do you have a specific issue you'd like help with? Commented Sep 9, 2015 at 2:33

4 Answers 4

7
undefined == null
// => true

undefined === null
// => false

== tests for equality, === tests for identity (or strict equality). If in doubt, use ===.

Sign up to request clarification or add additional context in comments.

7 Comments

That'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.
@squint: Indeed, as summaries generally are. Which is why I linked to the documentation, which is much more specific and exhaustive.
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.
@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" and "strict equality", not too different).
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.
|
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

Comments

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

5 Comments

It 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.
@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..
Doug 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.
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.
@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.
0

In JavaScript, the == and === operators are used to compare values, but they behave differently:

console.log(null == undefined); // true

Reason: When using ==, JavaScript considers null and undefined to be equal because they are both treated as "empty" or "non-existent" values in this context. Essentially, == compares the values after converting them to a common type, and for null and undefined, they are considered equivalent.

=== Operator (Strict Equality)

  • The === operator is known as the strict equality operator.
  • It does not perform type conversion and compares both the value and the type.
console.log(null === undefined); // false

Reason: When using ===, JavaScript checks both the type and the value without converting either operand. Since null is of type object and undefined is of type undefined, they are not strictly equal, and the comparison returns false.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.