Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined and null

Tags:

javascript

undefined === null => false
undefined == null => true
  1. I have thought about the reason of undefined == null and found out only one case:

    if(document.getElementById() == null) ....
    

    Is there any other reason to make (undefined === null) == false ?

  2. Is there any other examples of use === - operation in javascript?

like image 343
ceth Avatar asked May 17 '11 13:05

ceth


People also ask

Why is null == undefined true?

undefined is a primitive type of a variable which evaluates falsy, has a typeof() of undefined, and represents a variable that is declared but missing an initial value. null == undefined evaluates as true because they are loosely equal.

Is null or undefined better?

null is an assigned value. It means nothing. undefined typically means a variable has been declared but not defined yet. null and undefined are falsy values.

Is undefined a null value?

The undefined value is a primitive value used when a variable has not been assigned a value. The null value is a primitive value that represents the null, empty, or non-existent reference. When you declare a variable through var and do not give it a value, it will have the value undefined.

Is undefined null or empty?

Null is an assignment value, which means that you can assign the value null to any variable when you want that variable to be empty. It is intentionally left blank and will point to an empty value. Undefined is a variable that exists but hasn't been initialized YET.


4 Answers

Is there any other reason to make (undefined === null) == false ?

They are not equal so the Strict Equality Comparison Algorithm considers them to be false.

Is there any other examples of use === - operation in javascript?

The === gives the most predictable result. I only use == when I have a specific purpose for type coercion. (See Abstract Equality Comparison Algorithm.)

like image 82
user113716 Avatar answered Oct 05 '22 20:10

user113716


null and undefined are two different concepts. undefined is the lack of value (if you define a variable with var without initializing it, it doesn't contain null, but undefined), while with null the variable exists and is initialized with the value null, which is a special type of value.

JavaScript equality operator is broken though, Crockford found out that it lacks transitivity and for this reason suggests to use always the strict equality (===). Consider this table published in Javascript the good parts:

'' == '0'          // false
0 == ''            // true
0 == '0'           // true

false == 'false'   // false
false == '0'       // true

false == undefined // false
false == null      // false
null == undefined  // true
like image 42
stivlo Avatar answered Oct 05 '22 20:10

stivlo


=== is strict equal.

Undefined and null are not the same thing.

== uses type coercion.

null and undefined coerce to each other.

like image 24
Raynos Avatar answered Oct 05 '22 19:10

Raynos


Type coercion (using the == operator) can lead to undesired or unexpected results. After following all the talks I could find of Douglas Crockford on the net (mostly Yahoo video) I got used to using === all te time. Given my default usage of the strict equal operator I would be more interested in type coercion javascript use cases ;~) nowadays.

like image 28
KooiInc Avatar answered Oct 05 '22 19:10

KooiInc