Skip to main content

Difference between undefined and null

Today we are gonna talk about the famous question in Javascript which all of us must have faced in interview.

The difference between undefined and null.

But before we talk about their differences, lets first think, why are we comparing undefined and null and why not undefined and string or string and null?

So there must be something common,in general, between undefined and null. And that commonality is both of have them represents NO VALUE for the variable.

You can also call this NO VALUE as void or empty.

So basically when we say as variable is undefined or null that mean the value doesn't exist for the variable. Lets see how is it so.

1.   undefined == null     // true, so the value is same

Now lets check each one of them.First lets talk about undefined.

When do we get this undefined? Generally when we declare a variable but doesn't initialise it we get undefined. e.g

1.   var foo;
2.   console.log(foo);   // undefined

Also when we try to access a variable before its initialisation. e.g.

1.   console.log(foo);   //undefined
2.   var foo = 5;

NOTE: In the above example, undefined is due to hoisting. Checkout the hoisting article.

Also there is one more scenario,

1.   var man = {name: "Alex", age: 27};
2.   console.log(man["working"]);   // undefined

here we get undefined because the property "working doesn't exist for "man" object.

So conclusion from the above examples is undefined is something that is assigned by Javascript engine to represent NO VALUE and not by the programmer.

Now let see null, null is something that is not assigned by Javascript engine,it is something that is assigned by the programmer. e.g.

1.   var foo = null;
2.   console.log(foo);  // null

Second difference between undefined and null is related to there datatype

1.   var foo;
2.   console.log(typeof foo);   // undefined
3.   var bar =null;
4.   console.log(typeof bar);   // object

from the above example we can say, the datatype of undefined is undefined whereas datatype of null is object. 

So from the above conclusion we can draw that if we compare undefined and null with === we will get false

1.   undefined == null    // true


1.   undefined === null  // false


1.   typeof undefined    //  undefined
2.   typeof null         //   object

isNan(1 + undefined)   //   true
isNan(1 + null)        //   false

Comment your questions,if any.

Thank you


Post a Comment

Popular Posts

Come Closure in Javascript

What is a closure? If we want to derive a definition,we can say, A closure is a feature of Javascript where inner function has access to the outer (enclosing) function’s variables—scope chain. This scope chains means inner function has: access to its own scope (variables defined between its curly brackets and its parameters),access to the outer function’s variables and parameters, access to the global variables. Now we will look into it in detail. Consider following example
var globalVar = "xyz"; function outerFunc(outerArg) { var outerVar = 'Alex'; function innerFunc(innerArg) { var innerVar = 'Chris'; console.log("globalVar = " + globalVar); console.log("outerArg = " + outerArg); console.log("innerArg = " + innerArg); console.log("outerVar = " + outerVar); console.log("innerVar = " + innerVar); } innerFunc(456); } outerFunc(123); In this example, i…

Javascript Hoisting Interview Question and Answers - 5

In this post,I have tried to cover all the types of possible interview questions and answers on hoisting. If you are not familiar with the hoisting, I would highly recommend you to go to my post on hoisting. Once you are comfortable with the topic, you can attempt these questions. Understanding the type of question will help you to answer. I have provided the hint after each question. All the answers are given at the end of the post.
//Question 1 console.log('bar:', bar) bar = 15 var foo = 1 console.log(foo, bar) var bar Hint: Basics of hoisting //Question 2 var foo = 5 console.log('foo:', foo) var foo; var bar = 10; var bar; console.log('bar:', bar) var baz = 10 var baz = 12 console.log('baz:', baz) Hint: Basics of hoisting //Question 3 function foo() { function bar() { return 5 } return bar() function bar() { return 10 } } console.log(foo()); Hint: Think what happens if two functions wit…

Javascript Interview Question and Answer - 1

In this article we are going to discuss 2 javascript interview question and their answers.These questions are highly asked in the Javascript interviews.

Disclaimer: - You might have different solution working with all test cases for both the problems.

So lets discuss the first question.This question is to test your ability in terms javascript object knowledge and your logical thinking.

Q1. You have been given a nested object "Person" which has one of the property as "password".Write a generic function to change the value of all the "password" property to "*****". Object is nested and may contain other object with property "password" and so on. Here is a sample object.

var Person = {
     name: 'Alex',
     age : 40,
    password : 'abc',
    x : {
       name : 'Chris',
       password: 'abcd',
       y : {
          name : 'Mike',
          password: 'abcd',

Before we …