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
Above we have seen an example of how to check if a variable or object is null in Javascript.