Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Null in Javascript?
2.1.
javascript
3.
What is the Undefined in Javascript?
3.1.
javascript
3.2.
javascript
4.
Difference Between Null and Undefined In Javascript
5.
Null VS Undefined in Javascript.
5.1.
Arithmetic Operation on null and undefined.
5.1.1.
For Undefined
5.2.
javascript
5.2.1.
For Undefined 
5.3.
javascript
5.4.
Comparison using strict equality operator (===)
5.5.
javascript
6.
Frequently Asked Questions
6.1.
Why null == undefined is true in Javascript?
6.2.
Does NULL === undefined?
6.3.
When should we use null or undefined in JavaScript?
6.4.
Can we use the ‘typeof’ operator to check if a variable is null?
6.5.
How can we assign a default value to a variable that may be null or undefined?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference Between Null And Undefined In Javascript

Author Lakshya Gupta
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

Hello, Ninjas!! We are back with an exciting new article demonstrating the difference between Null and Undefined in Javascript. Do you know what NULL and Undefined are in Javascript? What is the difference between Null and Undefined in Javascript? If all these things and concepts are new to you, don't worry; we Coding Ninjas are here to help you and clear all Your doubts.

If you want to learn about more javascript concepts, check out this page.

Difference Between Null And Undefined In Javascript

In Javascript, Null and Undefined are two different types. Both seem very similar if we see them from an abstract level, as both represent the absence of value but have different uses and meanings. In this article, we will understand the Null, Undefined in Javascript and the difference between the Null and Undefined in Javascript that will help us to write clean, efficient, and bug-free JS code.

What is Null in Javascript?

Null, this term itself has its meaning, which we use to represent any quantity having no value. In Javascript, null is a primitive datatype and is used to assign it to variables or objects to represent the absence of a value. 

Null is an assignable value, and the developer intensionally assigns it to a variable to show the absence of the value of that variable.

Let us understand Null using some example code. 

Code:

  • javascript

javascript

let myName = null; 
console.log(myName);   


let student = {
   name: 'Rahul',
   // Values in an object can also be assigned as null.
   age: null          
};
console.log(student.age);    
console.log(typeof(student));   

 


Output:

null
null
object

 

Explanation:

Here, we assigned null to the ‘myName’ variable. We can assess it using the console.log() method, and it will show us the output as null. Then we created an object with properties such as ‘name’ and ‘age,’ where the ‘age’ property of the object student is null, showing that the student’s age is unknown to the developer.

The type of this variable is also checked using the typeof operator, and it has returned us object indication null is of type object.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is the Undefined in Javascript?

Undefined means something is ‘not defined.’ So in javascript, when we declare a variable and do not assign it any value, it is called an undefined variable.

In Javascript, undefined is a primitive data type. And it is automatically assigned by Javascript to the variable if we forget to assign any value to a variable.

Let’s understand it using some coding examples.

Code:

  • javascript

javascript

let username;
console.log(username);   
console.log(typeof(username)); 

 

Output:

undefined
undefined

 

Explanation:

Functions that do not have a return statement also return undefined.
 

Code:

  • javascript

javascript

function greetUser(username) {
   let name = username || 'Stranger';
   console.log(`Hey!! ${name}. How are you?`);
}


let result = greetUser('Lakshya'); 
greetUser();  


console.log(result); 
console.log(typeof(result)); 

Output:

Hey!! Lakshya. How are you?
Hey!! Stranger. How are you?
undefined
undefined

 

Explanation:

In this code example, we created a function named greetUser to greet users, which takes a username as input. Our function will use the default name if any user does not provide their name. We have printed the output on the console in both cases.

Also, we printed different results on the console. The function's return value is undefined since the function does not have any return statement, and the type of undefined is also undefined.

Difference Between Null and Undefined In Javascript

S.No.

Null

Undefined

1. It represents the intentional absence of a value. It represents the missing or uninitialized value.
2. It is an assignable value. It is automatically assigned to a variable or object that is not initialized. 
3. It is a primitive value in Js. It was introduced in ECMAScript1 (ES1). 
4. typeof operator returns ‘object.’  typeof operator returns ‘undefined.’
5. During arithmetic operations, null is converted to Zero(0). While performing arithmetic operationsundefined is converted to NaN.

Null VS Undefined in Javascript.

Null vs Undefined

Arithmetic Operation on null and undefined.

During arithmetic operations, null is operated as ‘0’. The Js compiler converts the null to a numerical value, and we can successfully do the arithmetic operations between any number and null. 

For Undefined

 

Code:

  • javascript

javascript

let x = 3;
let y = null;

console.log(x + y);    
console.log(typeof(x + y));   


Output:

3
number

 

Explanation:

Here we have declared two variables, ' x' and 'y', with 'x' assigned with a value of '3' and 'y' having a null value. The first console log statement prints the addition of 'x' and 'y', where the null will convert to 0 during the arithmetic operation. The second console log prints the type of return we are getting, i.e., number.

For Undefined
 

Code:

  • javascript

javascript

let x = 3;
let y;

console.log(x + y); 
console.log(typeof(x + y)); 

 

Output:

NaN
number


Explanation:

Here again, we have two variables, ' x' and 'y', with 'x' assigned with a value of '3' and 'y' does not have any value. The first console log statement prints the addition of 'x' and 'y' where the value of the undefined variable will convert to NaN during operation. The second console log prints the type of return we are getting, i.e., number.

Comparison using strict equality operator (===)

Code:

  • javascript

javascript

console.log(null === undefined); 
console.log(null == undefined); 

 

Output:

false 
true

 

Explanation:

Since the strict equality operator (===) compares the values of the variables as well as their types and returns false accordingly. But if we compare it using the loose equality operator (==), it will return true. 

Frequently Asked Questions

Why null == undefined is true in Javascript?

null == undefined is true because JavaScript's loose equality allows type coercion.

Does NULL === undefined?

null === undefined is false because it compares both value and type strictly.

When should we use null or undefined in JavaScript?

Null is used when we want to show any variable's missing or unknown value. At the same time, Undefined is a predefined Javascript type assigned to a variable or object that has not been assigned.

Can we use the ‘typeof’ operator to check if a variable is null?

No, we can not use the typeof operator to check this because it will return the object as the type for null. We should use the strict equality operator (===) to check if a variable is null. 

How can we assign a default value to a variable that may be null or undefined?

Using the OR operator, we can assign a default value to the variable or object that may be null or undefined. Ex. let name = null; name = name | |  ‘stranger’ .

Here, ’stranger’ will be the value of the name variable if the name is null or unfined.

Conclusion

Null and undefined are two very similar-looking terms in javascript as both represent the absence of a value. In this article, we have discussed both terms very detailedly. Also, we have compared them on a different basis. In the end, we have prepared a table that shows the difference between null and undefined in Javascript.

We recommend you to read these articles related to this article:

  1. Variables and Data Types in Javascript
     
  2. Data Types in C++
     
  3. Type Conversion in Javascript
     
  4. Explain "this" keyword and the Scope of this in Javascript Arrow Functions.
     
  5. Difference Between Compiler and Interpreter and Assembler
     

We hope you enjoyed the article. Remember to upvote if you liked it.

To ease your placement preparation journey, you can check out DSADBMSCompetitive ProgrammingPythonJavaand JavaScript courses.

You can also check out our Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews to crack your interviews easily. 

Happy Learning!! 

Previous article
JavaScript console.assert() Method
Next article
Regular Expressions in JavaScript
Live masterclass