By equality Operator (===)
By this operator, we will learn how to check for null values in JavaScript by the (===) operator. This operator only passes for null values, not for undefined, false, 0, NaN.
Syntax:
x === y;
Example: The following code snippets show some comparison of objects.
Javascript
const object1 = { key: "value" , }; const object2 = { key: "value" , }; console.log(object1 === object2); console.log(object1 === object1); |
false true
How to check for null values in JavaScript ?
The null values show the non-appearance of any object value. It is usually set on purpose to indicate that a variable has been declared but not yet assigned any value. This contrasts null from the similar primitive value undefined, which is an unintentional absence of any object value. That is because a variable that has been declared but not assigned any value is undefined, not null.
Below are the approaches:
Table of Content
- By equality Operator (===)
- By Object.is() function
- By the typeof Operator
- Using Lodash _.isNull() Method