Open In App
Related Articles

How to check if a Variable Is Not Null in JavaScript ?

Improve Article
Save Article
Like Article

In this article, we are going to learn how can we check if a Variable Is Not Null in JavaScript.

These are the following ways to solve this problem:

Method 1: Using if else

In this approach, we will use the if-else method to solve the problem. we will check whether the value is null or not by passing the value into the if condition.

Note: When a variable is null, there is an absence of any object value in a variable. Null is often retrieved in a place where an object can be expected, but no object is relevant.

By the above condition, if my_var is null then the given if condition will not execute because null is a falsy value in JavaScript, but in JavaScript, there are many pre-defined falsy values like

  • undefined
  • null
  • 0
  • “” ( empty string)
  • false
  • NaN

So if my_var is equal to any of the above pre-defined falsy values then if the condition would not execute or vice-versa. 

Example: This example shows the use of the above-explained approach.


let GFG_Var = "hello"
if (GFG_Var) {
    console.log("It is not null");
else {
    console.log("It is null");


It is not null

Method 2: Using Lodash _.isNull() Method

In this approach, we are using the library of javascript. we are using the _.isNull() method which returns true or false according to the given value. If the value is not null then it will return false. It will return the true if the given value is null.

Example: This example shows the use of the above-explained approach.


// Requiring the lodash library
const _ = require("lodash");
// Use of _.isNull()
// method
let gfg = _.isNull(null);
let gfg1 = _.isNull(void 0);
// Printing the output



Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 29 Nov, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials