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

Our focus lies on understanding how to verify if a variable is not null in JavaScript. We’ll explore several methods to tackle this issue effectively.

These are the following ways to solve this problem:

Table of Content

  • Using if else
  • Using Lodash _.isNull() Method
  • Using the typeof Operator:

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.

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

Output
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.

Javascript
// Requiring the lodash library 
const _ = require("lodash");

// Use of _.isNull() 
// method 
let gfg = _.isNull(null);
let gfg1 = _.isNull(void 0);

// Printing the output 
console.log(gfg);
console.log(gfg1);

Output:

true
false

Method 3:Using the typeof Operator:

Using the typeof operator, JavaScript checks if a variable is not null by verifying typeof variable !== ‘undefined’ && variable !== null. This approach ensures the variable exists and is not explicitly set to null, promoting reliable code execution.

Example: In this example we are using typeof operator with !== ‘undefined’ and !== null checks if a variable is not null in JavaScript. Output “Variable is not null” if true, else “Variable is null”.

JavaScript
// Case 1: Variable is not null
let variable = "Hello";

if (typeof variable !== 'undefined' && variable !== null) {
    console.log("Variable is not null");
} else {
    console.log("Variable is null");
}

// Case 2: Variable is null
variable = null;

if (typeof variable !== 'undefined' && variable !== null) {
    console.log("Variable is not null");
} else {
    console.log("Variable is null");
}

Output
Variable is not null
Variable is null

Method 4: Using the Double Equals (==) Operator

In JavaScript, the double equals (==) operator checks for equality, with type coercion. This means it will treat null and undefined as equal, which can simplify the null-checking process.

Using variable != null will check if a variable is neither null nor undefined, making this a concise and effective method for null-checking.

Example: This example demonstrates how the == operator can be used to check if a variable is not null or undefined:

JavaScript
// Case 1: Variable is not null or undefined
let variable1 = "Hello";

if (variable1 != null) {
    console.log("Variable is not null or undefined");
} else {
    console.log("Variable is null or undefined");
}

// Case 2: Variable is null
let variable2 = null;

if (variable2 != null) {
    console.log("Variable is not null or undefined");
} else {
    console.log("Variable is null or undefined");
}

// Case 3: Variable is undefined
let variable3;

if (variable3 != null) {
    console.log("Variable is not null or undefined");
} else {
    console.log("Variable is null or undefined");
}

Output
Variable is not null or undefined
Variable is null or undefined
Variable is null or undefined





Contact Us