Difference Between Null and Undefined In JavaScript

In this tutorial, I am going to explain the concept of Null and Undefined in JavaScript. This detailed blog will cover the following topics as follows

  1. Introduction
  2. What is Null in JavaScript?
  3. What is Undefined in JavaScript?
  4. Difference between Null and Undefined in JavaScript
  5. Bonus Question
  6. Conclusion

What is Null in JavaScript?

In JavaScript, null is a primitive value specified by the programmer that indicates the intentional absence of an object value. It is often used as a default value to indicate that no value should be assigned to a variable.

Key Points

  • Null is a primitive value.
  • Null and undefined are not strictly the same. (null === undefined //false)
  • Type: The type of `null` is an object. For a variable with a null value, the typeof() operator returns the type as an object.
console.log(typeof null);    //Output: object

Examples

The examples in this section demonstrate the working of null. Let's see.

1. The given example clearly shows that a null value is assigned to a variable.

let myVar = null;
console.log(myVar);    //Output: null

2. In the following example, we will get the result 1. As the variable is initialized to null and passed through arithmetic operations, it gets converted to zero.

var exp=null;
var base=21;
console.log(base**exp);    //Output: 21**0=1

3. Another example is presented to explain the primitive operation on null.

function Area(l,b,h) {
    h = null;
    var area =l*b*h;
    console.log(area);    //Output: 0
    console.log(h);      //Output: null
  }
Area(5,5);

What is Undefined in JavaScript?

Undefined is a primitive value that is automatically assigned by JavaScript to variables that have just been declared but not initialized or to function parameters that have not been provided. It is the default value of variables that have been declared but not initialized.

Key Points

  • Undefined is a primitive value.
  • Undefined and null are not strictly equivalent. (undefined === null //false)
  • Type: The type of `undefined` is itself undefined. The typeof() operator returns "undefined" for an undefined variable.
console.log(typeof undefined);    //Output: undefined

Examples

The examples in this section demonstrate the working of undefined. Let's see.

1. The given example clearly shows that a variable has been declared, but no value has been assigned to it yet.

let myVar;
console.log(myVar);    //Output: undefined

2. In the following example, we will get the result NaN. Since the variable has been declared, but no value has been assigned to it yet.

var exp;
var base=21;
console.log(base**exp);    //Output: NaN

3. Another example is presented to explain the primitive operation on undefined.

function Area(l,b,h) {
    var area = l*b*h;
    console.log(area);    //Output: NaN 
    console.log(h);       //Output: undefined
  }
Area(5,5);

4. To formal parameters of a function without actual arguments.

function Employee(myVar) {
  console.log(myVar);    // undefined if no argument is passed
}
Employee();

Null vs Undefined in JavaScript

The null and undefined are often confusing for beginners and experienced alike, but they serve different purposes. Now, let's look at the quick difference between Null and Undefined in JavaScript.

S.No. Key Points Null Undefined
1  Definition Null refers to the intentional absence of any object value. Undefined refers to a variable that has been declared but has not yet been assigned a value.
2  Type Null is an object type. The type of undefined is undefined.
3  Initialization It must be explicitly assigned to a variable. It is automatically assigned by JavaScript to variables that are declared but not initialized.
4  Loose Equality Comparison Both null and undefined are loosely equivalent.
null == undefined //true
Both null and undefined are loosely equivalent.
undefined == null //true
5  Strict Equality Comparison Both are not exactly the same.
null === undefined //false
Both are not strictly equivalent.
undefined === null //false
6  Primitive Operations During primitive operations, the null value is converted to zero (0). During primitive operations, the undefined returns NaN.
7  Is it an assignment value? Yes, as we assign null to a variable, it is an assignment value. No, as there is no value assigned to the variable, it becomes undefined.


What should I use as a default value for a variable, undefined or null?

When you want to indicate that no value should be set for a variable, it is usually recommended to use null as its default value. This is because undefined is the default value for uninitialized variables.

See you in the next blog, till then, take care and be happy learning.

You can connect with me @

Conclusion

In this blog, we have discussed the difference between null and undefined in JavaScript.

I hope you enjoyed this blog. Follow C# Corner to learn more new and amazing things about JavaScript.

Thanks for reading.