. Advertisement .
..3..
. Advertisement .
..4..
Anything that can change is considered variable. A variable in JavaScript saves the data value that allows for future data value changes. In JavaScript, there are two different sorts of variables: local variables and global variables. In this article, we will give you some solutions to check if a Variable is Not NULL in JavaScript. Read on it to open your knowledge.
How to check if a Variable is Not NULL in JavaScript
Solution 1: Utilize the strict inequality (!==) operator
To check if a Variable is Not NULL in JavaScript, you can utilize the strict inequality (!==) operator, for example, myVar !== null
. If the variable is not equal to null, the strict inequality operator will return true; otherwise, it will return false. Look at the following example to further understand about this method:
Code:
const var = 'How to check if a Variable is Not NULL in JavaScript';
if (var !== null) {
console.log(' var is NOT null');
}
else {
console.log(' var is null');
}
Output:
var is NOT null
The strict inequality (!==) operator is used to check if the value kept in the variable “var” is not equal to null.
The result of the operator is boolean:
If the values are not equal, the result returns true. If the values are similar, it returns false.
console.log(null !== null);
console.log('Welcome to Ittutoria.net' !== null);
Output:
false
true
In contrast to the loose inequality (!=) operator, the stringent inequality (!==) operator treats two values of different types as distinct. As a result, the strict inequality (!==) operator will always return false when comparing null with any other type. A typical error is to verify a value’s truthiness rather than checking whether it isn’t null or not. Below is an instance.
const var1 = 'Check if a Variable is Not NULL in JavaScript';
if (var1) {
console.log( var1 is NOT
false, 0, empty string, null, undefined, NaN);
} else {
console.log( var1 is ONE OF
false, 0, empty string, null, undefined, NaN);
}
In this example, we determine the truth of the value kept in the variable var1. All values that are not false are truthy. JavaScript’s false values are false, 0, “”, null, undefined, and NaN. The value of var1 is false if the else block is executed, and it may be one of the other six false values rather than null. When doing this, there are so many possible ways that things could go wrong. For instance, the else block will be activated if a value is equal to 0.
const var1 = 0;
if (var1) {
console.log("Check if a Variable is Not NULL");
} else {
console.log('Welcome to Ittutoria.net');
}
Output:
Welcome to Ittutoria.net
Even if this is what you want, writing code this way is never a good idea because it is unclear and indirect.
Rather than, you should be more apparent:
const var1 = 0;
if (var1 === 0) {
console.log("Check if a Variable is Not NULL");
} else {
console.log('Welcome to Ittutoria.net');
}
Output:
Check if a Variable is Not NULL
Utilizing stringent operators (!==, ===) when comparing equality values is your best solution. They help your program more apparent, readable, and the error less appears in your program.
Solution 2: Utilize Lodash
In addition to the built-in operators, you have the option to use other libraries. Although importing an external library to do this check is unnecessary, you would be better off using the operators in that scenario.
However, Lodash is already present in many projects because it is a commonly used library, and when it is present, using a few of its methods doesn’t reduce efficiency. In particular, Lodash provides several helpful methods for determining whether a variable is null, undefined, or nil.
Through a CDN, Lodash can be imported as the following:
https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js
Locally import it from a.js file:
<script src="lodash.js"></script>
Or set up it through NPM:
$ npm install lodash
Additionally, it can be imported using the need() syntax or as an ES6 module:
import _ from 'lodash'; // OR const _ = require('lodash');
The library may now be used to determine whether a variable is null, undefined, or nil, where nil denotes both of the conditions mentioned above. It serves as the abbreviated version for verifying both:
let var2 = null;
console.log(.isNull(var2)); // true
console.log(.isUndefined(var2)); // false
console.log(_.isNil(var2)); // true
Conclusion
The solutions described above are the best solutions to check if a Variable is Not NULL in JavaScript. We hope this blog has helped clear the air around how to do it. And we believe that you will be very interested in the topic of this article. If you have more questions about this topic, please leave a comment below. Thank you for reading; we are always excited when one of our posts can provide useful information on a topic like this!
Read more
Leave a comment