If you’re working with JavaScript, you may need to check if a variable is not null. This can be done using the !== operator. In this tutorial, we’ll show you how to use it. We’ll also give some examples of how it can be used. Stay tuned!
!== Operator To Check If A Variable Is Not Null
The !== operator (strict inequality) is used to check if a variable is not equal to another value. It’s important to note that this operator checks for both value and type. So, if you’re checking if a variable is not null, the !== operator will return true if the variable is not null and false if it is null.
Here’s an example of how the !== operator can be used:
var myVar = "Hello!";
if (myVar !== null) {
console.log("myVar is not null");
} else {
console.log("myVar is null");
}
In the example above, we have a variable called myVar. We’re using the !== operator to check if myVar is not null. If it’s not null, we’ll print “myVar is not null” to the console. If it is null, we’ll print “myVar is null” to the console.
A Common Mistake – Checking For Truthy Values
A pretty common mistake that most people make when checking for null values is to check for truthy values to make sure a variable is not null. But, while the statement returns true for not null case, it also returns true when the variable is not –
- false
- 0
- empty
- undefined
- NaN
Let’s see an example of this:
var myVar = 0;
if (myVar) {
console.log("myVar is not null, false, 0, empty, undefined or NaN");
} else {
console.log("myVar is null or one of the falsey values");
}
In the example above, we have a variable called myVar. We’re using the if(variable) statement to check if myVar is not null. As you can see, this statement will also return true if myVar is 0 – which is not what we want. So, it’s important to use the !== operator when checking for null values.
Using The typeof Operator To Check If A Variable Is Not Undefined And Null
Another way to check if a variable is not null is to use the typeof operator. The typeof operator returns the data type of a variable – which can be helpful when you’re checking if a variable is null.
Here’s an example of how the typeof operator can be used:
var myVar = "Hello!";
if (typeof myVar !== "undefined" && myVar !== null) {
console.log("myVar is not null or undefined");
} else {
console.log("myVar is null or undefined");
}
In the example above, we have a variable called myVar. We’re using the typeof operator to check if myVar is not undefined. We’re also using the !== operator to check if myVar is not null. If both of these conditions are true, we’ll print “myVar is not null or undefined” to the console. If either of these conditions is false, we’ll print “myVar is null or undefined” to the console.
As you can see, there are a few different ways that you can check if a variable is not null in JavaScript. In most cases, it’s best to use the !== Not Null operator. But, if you’re working with a lot of data types, the typeof method may be a better option.
We hope you found this tutorial helpful!