Check null values in JavaScript?

JavaScript
null
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
}

or

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.

if(variable){
  // which means the variable is not null
}else{
  // the variable is null
}

We can also comparing null with 0


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

or

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

 

 

You might also like:

Simple Array Sum | Javascript | hackerrank.com

07-05-2020 hackerrank.com javascript arrays array sum

File Upload Using Angular Js and Node Js

28-05-2018 AngularJs Node js file upload multer module

How to use http-post request in node js

14-03-2017 http-post module node js

How to make an http request in node js

14-03-2017 http module node js

Send Mail in Node Js

21-02-2017 node js mail npm module