How to check if a Variable Is Not Null in JavaScript ?
Last Updated :
01 May, 2025
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");
}
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");
}
OutputVariable 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");
}
OutputVariable 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.
Similar Reads
How to Check if JSON Key Value is Null in JavaScript ?
In JavaScript, the JSON Object can contain null values, which should be identified. The below approaches can be used to check if the JSON key is null. Table of Content Using for Loop and '===' OperatorUsing Object.values() with Array.prototype.includes()Using Array.prototype.some()Using for Loop and
3 min read
How to Check if a Variable is an Array in JavaScript?
To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript
2 min read
How to Check if a Value is a Number in JavaScript ?
To check if a value is a number in JavaScript, use the typeof operator to ensure the value's type is 'number'. Additionally, functions like Number.isFinite() and !isNaN() can verify if a value is a valid, finite number. Methods to Check if a Value is a NumberThere are various ways to check if a valu
3 min read
How to check if a value is object-like in JavaScript ?
In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope
4 min read
Bash Scripting - How to check If variable is Set
In BASH, we have the ability to check if a variable is set or not. We can use a couple of arguments in the conditional statements to check whether the variable has a value. Â In this article, we will see how to check if the variable has a set/empty value using certain options like -z, -v, and -n. Usi
10 min read
How to check for null values in JavaScript ?
The null values show the non-appearance of any object value. It is usually set on purpose to indicate that a variable has been declared but not yet assigned any value. This contrasts null from the similar primitive value undefined, which is an unintentional absence of any object value. That is becau
4 min read
How to check if the value is primitive or not in JavaScript ?
To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results. Primitive data types are basic building blocks like numbers and characters, while non-primitive data
3 min read
How to Check if Object is JSON in JavaScript ?
JSON is used to store and exchange data in a structured format. To check if an object is JSON in JavaScript, you can use various approaches and methods. There are several possible approaches to check if the object is JSON in JavaScript which are as follows: Table of Content Using Constructor Type Ch
3 min read
How to check the type of a variable or object in JavaScript ?
In this article, How to Check the Type of a Variable or Object in JavaScript? In JavaScript, the typeof operator is used to determine the typeof an object or variable. JavaScript, on the other hand, is a dynamically typed (or weakly typed) language. This indicates that a variable can have any type o
2 min read
Check if a variable is a string using JavaScript
Checking if a variable is a string in JavaScript is a common task to ensure that the data type of a variable is what you expect. This is particularly important when handling user inputs or working with dynamic data, where type validation helps prevent errors and ensures reliable code execution. Belo
3 min read