Javatpoint Logo
Javatpoint Logo

How to check if a variable is not NULL in JavaScript

JavaScript is a popular programming language that is widely used in web development. One of the most common tasks in JavaScript is checking whether a variable has a value or not. A variable can have a value or null, which means that it doesn't have a value at all. It can cause errors in your code if you try to perform operations on a null variable. Therefore, it's important to check whether a variable is not null before using it.

In this article, we'll explore different ways to check if a variable is not null in JavaScript.

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

The simplest way to check if a variable is not null in JavaScript is to use the "!== null" operator. This operator checks if the value of a variable is not null. Here's an example:

The variable myVar is given the value null in this code. The if clause determines whether myVar does not equal null. The console will display "Variable is not null" if the condition is true (i.e., myVar is not null). Otherwise, it will print "Variable is null" to the console if the condition is false (i.e., myVar is null).

Output will be:

Variable is null

MyVar is given the value null, therefore the else block's code is performed because the condition myVar!== null evaluates to false, logging "Variable is null" to the console.

2. Using the "typeof" Operator:

Another way to check if a variable is not null is to use the "typeof" operator. The "typeof" operator returns the type of a variable. If a variable is null, the "typeof" operator will return "object". Here's an example:

In this code, the variable myVar is given the value null. The type of myVar is then verified using the typeof operator, which will yield 'object' for null in this case.

Two conditions are checked by the if statement:

typeofmyVar === 'object': This test determines whether myVar's type is 'object. Since 'object' is returned by typeof null, this is required. This requirement guarantees that myVar is an object and not some other type.

myVar!== null: This test determines whether or not myVar is equal to null. The typeof operator by itself is unable to discriminate between null and other objects, necessitating a further check.

MyVar will log "Variable is not null" to the console if both conditions, that is, that it is both an object and not null, are true. Otherwise, it will output "Variable is null" to the console if either of the conditions is false.

Output will be:

Variable is null

Explanation:

The first condition typeofmyVar === 'object' evaluates to true because myVar has the value null, but the second condition myVar!== null evaluates to false because myVar has the value null. As a result, the else block's code is run, and "Variable is null" is printed to the console.

3. Using the "null" Keyword:

You can also check if a variable is null using the "null" keyword. The "null" keyword represents the absence of any object value. Here's an example:

In this code, the variable myVar is given the value null. The if clause determines whether myVar does not equal null.

MyVar is not null if the condition myVar!== null evaluates to true. The if block's function will then be run if that is the case. The comment "// do something" in the example code denotes the code you might include within the if block to carry out the desired action when the variable is not null.

On the other hand, if the test for myVar!== null returns false, myVar is in fact null. The else block's function will then be run in that scenario. The variable is declared to be null in the provided code, which logs "Variable is null" to the console.

Output will be:

Variable is null

Explanation:

The condition myVar!== null evaluates to false because the value null has been assigned to myVar. As a result, the else block's code is run, and "Variable is null" is printed to the console.

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

The double exclamation mark is another way to check if a variable is not null in JavaScript. The double exclamation mark is used to convert a value to a Boolean value. If a variable is not null, the double exclamation mark will return "true". Here's an example:

In this code, the variable myVar is given the value null. The variable is subjected to double negation using the !! operator. This method of transforming a value into its matching boolean representation is frequently utilized.

The double negation will yield true if the variable myVar is true. The double negation of null will return false in JavaScript since null is regarded as false.

Following the double negation and before checking the boolean value of myVar, the if statement. The code inside the if block will be performed if the value of the variable, myVar, is true and neither null nor false.

In contrast, if the value is false, myVar is empty or false. The else block's function will then be run in that scenario. The variable is declared to be null in the provided code, which logs "Variable is null" to the console.

Output will be:

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 is a shorthand way of writing an "if" statement. It's a one-liner that can be used to check if a variable is not null. Here's an example:

In this code, the variable myVar is given the value null. The condition myVar!== null is checked using the ternary operator?:. MyVar is not null if the condition myVar!== null evaluates to true. In that situation, console.log('myVar is not null') will be used to execute the expression that comes before the?.

On the other hand, if the test for myVar!== null returns false, myVar is in fact null. In that situation, console.log('myVar is null') will be used to execute the expression that follows the:.

Output will be:

myVar is null

Explanation:

The condition myVar!== null evaluates to false because the value null has been assigned to myVar. The expression following the: is therefore run, and "myVar is null" is printed to the console.

Conclusion:

In this article, we've explored different ways to check if a variable is not null in JavaScript. Whether you choose to use the "!== null" operator, the "typeof" operator, the "null" keyword, the double exclamation mark, the ternary operator, or optional chaining, it's important to always check if a variable is not null before using it to avoid errors in your code.

By understanding these different techniques, you'll be better equipped to write more reliable and error-free JavaScript code. So next time you're working with variables in JavaScript, remember to always check if they're not null before using them.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA