How to check if a variable is not NULL in JavaScript

JavaScript, a widely utilized programming language in web development, frequently involves verifying the presence of a value in a variable. Variables in JavaScript can either hold a value or be null, indicating the absence of a value. Attempting operations on a null variable can lead to errors in your code. Hence, it is crucial to verify that a variable is not null prior to utilizing it.

This guide will delve into various methods for verifying the absence of null in a variable within JavaScript.

1. Using the "!== null" Operator:

To verify if a variable is not null in JavaScript, you can employ the " !== null" operator. This operator evaluates whether the variable's value is indeed not null. See the following demonstration:

Example

let myVar = null;

if (myVar !== null) {

console.log("Variable is not null");

} else {

console.log("Variable is null");

}

In this code snippet, the variable named myVar is assigned the value null. An if statement is used to check if myVar is not equal to null. If the condition evaluates to true, the message "Variable is not null" will be shown in the console. Conversely, if the condition evaluates to false, indicating that myVar is null, the console will display "Variable is null".

Output will be:

Output

Variable is null

Given that the variable MyVar is assigned the value of null, the code within the else block is executed as the condition MyVar !== null is false. This results in the message "Variable is null" being logged to the console.

2. Using the "typeof" Operator:

An alternative method to validate the absence of a value in a variable involves utilizing the "typeof" operator. This operator is responsible for indicating the type of a given variable. In cases where a variable is null, the "typeof" operator will yield a result of "object". Below is an illustration:

Example

let myVar = null;

if (typeofmyVar === 'object' &&myVar !== null) {

console.log("Variable is not null");

} else {

console.log("Variable is null");

}

Within this code snippet, the variable named myVar is explicitly assigned the value null. Following this assignment, the typeof operator is employed to determine the data type of myVar. In this scenario, the typeof operator will return 'object' for the value null.

Two conditions are checked by the if statement:

The condition typeof myVar === 'object' is used to verify if the type of myVar is specifically 'object'. This check is necessary because the typeof operator returns 'object' for null values. Ensuring this condition guarantees that myVar is indeed an object and not a different data type.

The expression myVar!== null is used to check if myVar is not equal to null. When using the typeof operator alone, it is unable to distinguish between null and other objects, hence the need for an additional verification.

If the conditions that MyVar is an object and is not null are both met, the console will display "Variable is not null". If either of these conditions is not true, the console will show "Variable is null".

Output will be:

Output

Variable is null

Explanation:

The initial comparison typeofmyVar === 'object' returns true due to the null value assigned to myVar. Conversely, the subsequent comparison myVar !== null returns false for the same null value. Consequently, the code within the else block is executed, leading to the output "Variable is null" displayed on the console.

3. Using the "null" Keyword:

You can verify the absence of a value in a variable by using the keyword "null". The "null" keyword signifies the lack of any object value. Here is an illustration:

Example

let myVar = null;

if (myVar !== null) {

console.log("Variable is not null");

} else {

console.log("Variable is null");

}

Within this code snippet, the variable myVar is assigned the null value. The conditional statement checks if myVar is not equal to null.

When the condition myVar !== null is true, it indicates that MyVar is not null. In such a scenario, the code inside the if block will be executed. In the provided code example, the comment "// do something" signifies the section where you can insert the necessary code to perform the intended action when the variable is not null.

Conversely, when the condition myVar !== null evaluates to false, it indicates that myVar is null. In such a case, the code within the else block will be executed. The variable myVar is explicitly assigned a null value in the code snippet, resulting in the message "Variable is null" being displayed in the console.

Output will be:

Output

Variable is null

Explanation:

The expression myVar!== null results in a false evaluation due to the assignment of the value null to myVar. Consequently, the code within the else block is executed, leading to the output "Variable is null" being displayed in the console.

4. Using the Double Exclamation Mark (!!):

Using the double exclamation mark in JavaScript is a technique to verify if a variable is not null. It works by transforming a value into a Boolean representation. When applied, it will result in "true" if the variable is not null. Below is an illustration of this concept:

Example

let myVar = null;



if (!!myVar) {

console.log("Variable is not null");

} else {

console.log("Variable is null");

}

Within this code snippet, the variable myVar is explicitly assigned the value null. The variable undergoes a process of double negation through the use of the !! operator. This technique is commonly employed to convert a value into its corresponding boolean representation.

The expression of double negation will result in a true value when the variable myVar evaluates to true. Conversely, in JavaScript, the act of double negating null will produce a false outcome, as null is considered falsy in the language.

Prior to verifying the boolean value of myVar and after handling the double negation, the code enclosed within the if statement will execute only if the variable myVar holds a true value and is not null or false.

Conversely, when the value is false, myVar will be either empty or false. Consequently, the function inside the else block will be executed in this case. The variable is explicitly set to null in the code snippet, resulting in the message "Variable is null" being displayed in the console.

Output will be:

Output

Variable is null

Explanation:

MyVar is given the value null , thus the double negation !! is used, and the value of myVar is false . As a result, the else block's code is run, and "Variable is null" is printed to the console.

5. Using the Ternary Operator:

The ternary operator provides a concise alternative to the traditional "if" statement. It is a compact single line of code that serves the purpose of verifying if a variable is not null. Below is an illustration:

Example

let myVar = null;

myVar !== null ? console.log('myVar is not null') : console.log('myVar is null');

Within this script, the variable named myVar is assigned the null value. The script then utilizes the ternary operator ?: to validate the condition myVar !== null. If the outcome of this condition is true, indicating that myVar is not null, the statement console.log('myVar is not null') will be triggered to execute the preceding expression before the ?.

Conversely, when the condition myVar !== null evaluates to false, it indicates that myVar is indeed null. In such a scenario, the statement console.log('myVar is null') will be utilized to run the subsequent expression.

Output will be:

Output

myVar is null

Explanation:

The condition myVar!== null results in a false evaluation due to the assignment of the value null to myVar. Consequently, the subsequent expression after the: is executed, leading to the output "myVar is null" being displayed on the console.

Conclusion:

In this guide, we have discussed various methods for verifying the absence of a value in JavaScript. Whether you opt for the "!== null" comparison operator, the "typeof" operator, the "null" keyword, the double negation, the ternary operator, or optional chaining, it is crucial to consistently validate the non-null status of a variable prior to its usage to prevent potential errors in your code.

Having a grasp of these various methods will enhance your ability to produce JavaScript code that is more dependable and free of errors. When handling variables in JavaScript, it is crucial to consistently verify that they are not null before utilizing them.

Input Required

This code uses input(). Please provide values below: