How to use the Double Equals (==) Operator In Javascript
In JavaScript, the double equals (==) operator checks for equality, with type coercion. This means it will treat null and undefined as equal, which can simplify the null-checking process.
Using variable != null will check if a variable is neither null nor undefined, making this a concise and effective method for null-checking.
Example: This example demonstrates how the == operator can be used to check if a variable is not null or undefined:
// Case 1: Variable is not null or undefined
let variable1 = "Hello";
if (variable1 != null) {
console.log("Variable is not null or undefined");
} else {
console.log("Variable is null or undefined");
}
// Case 2: Variable is null
let variable2 = null;
if (variable2 != null) {
console.log("Variable is not null or undefined");
} else {
console.log("Variable is null or undefined");
}
// Case 3: Variable is undefined
let variable3;
if (variable3 != null) {
console.log("Variable is not null or undefined");
} else {
console.log("Variable is null or undefined");
}
Output
Variable is not null or undefined Variable is null or undefined Variable is null or undefined
How to check if a Variable Is Not Null in JavaScript ?
Our focus lies on understanding how to verify if a variable is not null in JavaScript. We’ll explore several methods to tackle this issue effectively.
These are the following ways to solve this problem:
Table of Content
- Using if else
- Using Lodash _.isNull() Method
- Using the typeof Operator:
Contact Us