Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript checking for null vs. undefined and difference between == and ===

  1. How do I check a variable if it's null or undefined and what is the difference between the null and undefined?

  2. What is the difference between == and === (it's hard to search Google for "===" )?

like image 885
MUG4N Avatar asked Feb 24 '11 08:02

MUG4N


People also ask

Does undefined === null in JavaScript?

In JavaScript, undefined is a type, whereas null an object. It means a variable declared, but no value has been assigned a value. Whereas, null in JavaScript is an assignment value.

IS NULL === undefined?

It means null is equal to undefined but not identical. When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.

What is the difference between == and === in JavaScript?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

Does NULL === null in JavaScript?

Javascript null is a primitive type that has one value null. JavaScript uses the null value to represent a missing object. Use the strict equality operator ( === ) to check if a value is null . The typeof null returns 'object' , which is historical bug in JavaScript that may never be fixed.


2 Answers

How do I check a variable if it's null or undefined...

Is the variable null:

if (a === null) // or if (a == null) // but see note below 

...but note the latter will also be true if a is undefined.

Is it undefined:

if (typeof a === "undefined") // or if (a === undefined) // or if (a == undefined) // but see note below 

...but again, note that the last one is vague; it will also be true if a is null.

Now, despite the above, the usual way to check for those is to use the fact that they're falsey:

if (!a) {     // `a` is falsey, which includes `undefined` and `null`     // (and `""`, and `0`, and `NaN`, and [of course] `false`) } 

This is defined by ToBoolean in the spec.

...and what is the difference between the null and undefined?

They're both values usually used to indicate the absence of something. undefined is the more generic one, used as the default value of variables until they're assigned some other value, as the value of function arguments that weren't provided when the function was called, and as the value you get when you ask an object for a property it doesn't have. But it can also be explicitly used in all of those situations. (There's a difference between an object not having a property, and having the property with the value undefined; there's a difference between calling a function with the value undefined for an argument, and leaving that argument off entirely.)

null is slightly more specific than undefined: It's a blank object reference. JavaScript is loosely typed, of course, but not all of the things JavaScript interacts with are loosely typed. If an API like the DOM in browsers needs an object reference that's blank, we use null, not undefined. And similarly, the DOM's getElementById operation returns an object reference — either a valid one (if it found the DOM element), or null (if it didn't).

Interestingly (or not), they're their own types. Which is to say, null is the only value in the Null type, and undefined is the only value in the Undefined type.

What is the difference between "==" and "==="

The only difference between them is that == will do type coercion to try to get the values to match, and === won't. So for instance "1" == 1 is true, because "1" coerces to 1. But "1" === 1 is false, because the types don't match. ("1" !== 1 is true.) The first (real) step of === is "Are the types of the operands the same?" and if the answer is "no", the result is false. If the types are the same, it does exactly what == does.

Type coercion uses quite complex rules and can have surprising results (for instance, "" == 0 is true).

More in the spec:

  • Abstract Equality Comparison (==, also called "loose" equality)
  • Strict Equality Comparison (===)
like image 90
T.J. Crowder Avatar answered Oct 03 '22 03:10

T.J. Crowder


The difference is subtle.

In JavaScript an undefined variable is a variable that as never been declared, or never assigned a value. Let's say you declare var a; for instance, then a will be undefined, because it was never assigned any value.

But if you then assign a = null; then a will now be null. In JavaScript null is an object (try typeof null in a JavaScript console if you don't believe me), which means that null is a value (in fact even undefined is a value).

Example:

var a; typeof a;     # => "undefined"  a = null; typeof null;  # => "object" 

This can prove useful in function arguments. You may want to have a default value, but consider null to be acceptable. In which case you may do:

function doSomething(first, second, optional) {     if (typeof optional === "undefined") {         optional = "three";     }     // do something } 

If you omit the optional parameter doSomething(1, 2) thenoptional will be the "three" string but if you pass doSomething(1, 2, null) then optional will be null.

As for the equal == and strictly equal === comparators, the first one is weakly type, while strictly equal also checks for the type of values. That means that 0 == "0" will return true; while 0 === "0" will return false, because a number is not a string.

You may use those operators to check between undefined an null. For example:

null === null            # => true undefined === undefined  # => true undefined === null       # => false undefined == null        # => true 

The last case is interesting, because it allows you to check if a variable is either undefined or null and nothing else:

function test(val) {     return val == null; } test(null);       # => true test(undefined);  # => true 
like image 24
Julien Portalier Avatar answered Oct 03 '22 03:10

Julien Portalier