undefined and null in JavaScript

undefined and null in JavaScript


I have been coding in C# from the beginning of my career and coming from C# background when I first heard of null and undefined in JavaScript I could think that 'null' in JavaScript would mean similar to what we have in other languages. Undefined was something which was very new to me and I used to consider both of them the same however there are differences between null and undefined. Let us go through them in this article.

Comparing null & undefined

null and undefined both of them are used to indicate the absence of a value and both of them are primitives in JavaScript which means they do not have any method that can be called on them unlike the object types or non primitive types.

Now here are few interesting things -

  1. When we do a typeof on null and undefined it returns the string 'object' for null and 'undefined' for 'undefined'
typeof null; // object
typeof undefined; // undefined

typeof on null and undefined

Notice that the typeof(null) returns 'object' and typeof(undefined) returns 'undefined'.

  1. null is a language keyword in JavaScript and its represents the absence of an object value however 'undefined' is a global property in JavaScript that defines the value of the property as 'undefined' unless it is initialized.

    let studentName;
    console.log(studentName); // returns undefined

    The above code would return undefined since the variable studentName was never assigned any value hence it would take the globally assigned value 'undefined'.

    null can be explicitly assigned to variables to denote that the variable does not point to anything.

  2. The '==' operator treats both null and undefined as same and returns true.

    null == undefined; //true

    The '===' operator treats both null and undefined as different and returns false.

    null === undefined; //false
  3. Both the null and undefined are falsy values. They behave as 'false' when boolean values are required. I have seen code where people put explicit checks to see if the value of a property is not equal to null and not equal to undefined.

    let objStudent = {}
     //explicit checks for null and undefined
     if(objStudent.name !=null && objStudent.name!=undefined)
     // The above code can be refactored as below
  4. Behavior of null and undefined with isNaN() null means nothing so if we pass null to the isNaN() method it would return false.

    isNaN(null); // returns false
    isNaN(2 + null); //returns false

    undefined means the global value undefined which is not a numerical value and hence the isNaN() returns true.

    isNaN(undefined); // returns true
    isNaN(2 + undefined); // returns true

    Behavior of null and undefined with isNaN() function

I hope I am able to explain the differences of null and undefined.

Book Recommendation

I would recommend you to refer to the book JavaScript - The Definitive Guide by David Flanagan for an in-depth understanding of the JavaScript Language.

Buy a coffee for sudshekhar