Open In App

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

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized. This check is important for avoiding errors when accessing or manipulating data, ensuring that the variable holds valid, usable content before proceeding with operations.

Methods

Below are the following methods by which we can check if a variable is not null in JavaScript:

1: Using if-else Statements

This approach uses an if statement to check if a variable is truthy, meaning it evaluates to a value other than null, undefined, 0, false, NaN, or an empty string. If the variable is truthy, the code executes the first block; otherwise, it runs the else block.

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

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

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

4: Using Strict Equality (!== null)

One of the most direct and simple ways to check if a variable is not null is to use the strict equality operator !==. This operator checks if the value is strictly not equal to null, ensuring there’s no type coercion.

Example: In this example we are using the strict equality (!==null).

JavaScript
let user = "anjali";
if (user !== null) {
    console.log("Variable is not null");
} else {
    console.log("Variable is null");
}

Output
Variable is not null

Best Practices for Null Checks

  • Always Use Strict Comparison (!== null): This avoids unintended type coercion and ensures accurate results.
  • Check for Undefined as Well: Sometimes variables may be undefined instead of null. To cover both cases, consider checking both null and undefined together.
  • Use Libraries When Needed: When working with complex data, consider using libraries like Lodash, which offer utility methods like _.isNull() for consistent and readable code.
  • Don’t Ignore Edge Cases: Always handle edge cases, such as when a variable might be an empty string, 0, or other falsy values that might be misinterpreted.

Common Use Cases for Null Checks

  • Form Validation: Ensuring that a form field is not null before submitting the form to avoid server errors.
  • API Responses: When making API calls, checking if the response is null can prevent errors when processing data.
  • User Inputs: If you're processing user-provided data, checking for null ensures that missing or invalid values don’t disrupt your code.
  • Object Properties: Before accessing properties on objects, make sure they are not null to prevent runtime errors.

Conclusion

In JavaScript, checking whether a variable is not null is an important step to ensure that your code runs smoothly. We explored various methods such as using if-else statements, Lodash's _.isNull() method, the typeof operator, and the strict equality (!== null) operator. These techniques help prevent runtime errors and ensure that your code handles variables correctly.


Next Article

Similar Reads

  翻译: