JavaScript: Check if null variable, object

How to check if a variable is null in JavaScript? How to check if an object is null in JS?

Check if the variable is null in JS

Here is an example of how to check if a variable is null. We can just use the variable inside an if condition to check if null.

Method 1:

Null check short method in JS.

let person = { name:'smartcoder' }

if(person){
   console.log(person.age);
}

Method 2:

let person = { name:'smartcoder' }

if(person !== null){
   console.log(person.age);
}

Check if the object is null in JS

How to check if a variable is null in JavaScript? How to check if an object is null in JS?

Above we have seen an example of how to check if a variable or object is null in Javascript.

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *