Check null values in JavaScript?

check null value

Null is a javascript one of the data type.

Below is the ways to test whether a value is null or not.

When we are initialize a variable in javascript, the default value is null

var variable;
console.log(variable) // null

If we are assigned null to a variable, the value of variable is null like below

var variable = null;
console.log(variable) // null

Check a variable is null or not using if condition.

if(variable == null){
  // write somthing


if(variable === null){
  // write somthing

The difference between above both conditions are “==” and “===”.

== is nothing but comparing two values only.

=== is nothing but comparing two values and type also.

We can also write condition like below for more accuracy.

if(variable === null && typeof variable === "object"){

Basically type of the null variable is object.

We can also write condition like.

  // which means the variable is not null
  // the variable is null

We can also comparing null with 0

if(null !== 0){
  // the value is true


if(null === 0){
  // the value is false



You might also like:

Functions in javascript

25-05-2018 functions javascript es5 es6 arrow functions named functions Closures Nested Functions Callbacks Arrow functions.

How to submit form using javascript or jquery.

16-04-2017 javascript jquery submit form

How to get id by using name attribute in javascript

20-03-2017 html id name javascript

How to set or update or push the values into Object in javacript

27-02-2017 javascript set update push object

Call a function every second or 5 seconds in javascript

22-02-2017 setinterval function calling