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 Ninjasare here to help you and clear all Your doubts.
If you want to learn about more javascript concepts, check out this page.
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));
You can also try this code with Online Javascript Compiler
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.
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));
You can also try this code with Online Javascript Compiler
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 operations, undefined is converted to NaN.
Null VS Undefined in Javascript.
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));
You can also try this code with Online Javascript Compiler
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));
You can also try this code with Online Javascript Compiler
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.
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: