There are some ways to check if a variable is null or undefined in JavaScript. Let’s learn about it with the explanation and examples below.
Check If Variable Is Null or Undefined In JavaScript
What are variables in JavaScript?
As many other languages, JavaScript also has variables. Variable means anything that can vary. A variable is a container to store data value.
The keywords var or let or const are used to declare a variable in JavaScript.
Look at the example below to learn more about variables in JavaScript.
var num = 10
var str = "Learn share IT"
var fl = 10.5
var x = true
var y
let num1 = 10
let str = "Learn to share IT"
let x1 = true;
const fl = 10.5
What are null value and undefined value?
The value null represents the intentional absence of any object value, and it is a primitive value.
Look at the example below.
var x= null // value is a null value
console.log(x)
Output
null
Undefined is a primitive value in JavaScript.
A variable that has not been assigned a value is of type undefined.
Look at the example below.
var x // variable x is a undefined value
console.log(x)
Output
undefined
What is the difference between null value and undefined value?
We often get confused about what the difference between UNDEFINED value and NULL value is.
Null and Undefined are also primitive values. But there are many differences between them.
Null represents a missing object, while an undefined value means lack of value.
We can assign the value null to the variable when you want it to be empty. Undefined is a variable that value did not have before.
Look at the example below to know more about it.
var a = null // variable a is null value
var b // variable b is undefined value
console.log(a)
console.log(b)
Output
null
undefined
Solution 1: Using equality operator
We can use == operator to check if a variable is null or undefined.
Syntax
variable_check == null / undefined
Parameters
- variable_check: The variable you want to check.
Return Value: true or false
Look at the example below to learn about it.
function check(variable_check) {
// check if value is null or not.
if (variable_check == null ) {
console.log("The variable is null");
}
else if (variable_check == undefined) {
// check if value is undefined or not.
console.log("The variable is undefined");
}
else {
console.log("The variable is not null and not undefined");
}
}
var a = null
var b
var c = 10
var d = 0.5
var e = true
var f = undefined
check(a)
check(b)
check(c)
check(d)
check(e)
check(f)
Output
The variable is null
The variable is null
The variable is not null and not undefined
The variable is not null and not undefined
The variable is not null and not undefined
The variable is null
Solution 2: Using typeof operator
Syntax
typeof variable_check ==’null’ / ‘undefined’
Parameters
- variable_check: The variable you want to check.
Return Value: true or false.
Look at the example below to know more about this solution.
function check(variable_check) {
// check if value is null or not.
if (typeof variable_check == 'null' ) {
console.log("The variable is null");
}
else if (typeof variable_check == 'undefined') {
// check if value is undefined or not.
console.log("The variable is undefined");
}
else {
console.log("The variable is not null and not undefined");
}
}
var a = null
var b
var c = 10
var d = 0.5
var e = true
var f = undefined
check(a)
check(b)
check(c)
check(d)
check(e)
check(f)
Output
The variable is null
The variable is null
The variable is not null and not undefined
The variable is not null and not undefined
The variable is not null and not undefined
The variable is null
Summary
You can use the equality operator or typeof operator to check if a variable is null or undefined in JavaScript. We hope this guide is helpful to you. Thanks!
Maybe you are interested:
- Check if Variable is of Function Type using JavaScript
- Check if a Variable is of type Error in JavaScript
- Check if a Variable is Not NULL in JavaScript

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R