Null in JavaScript
Null is a primitive type in JavaScript. This means you are supposed to be able to check if a variable is null in JavaScript with the typeof() method. Unfortunately, this returns an “object” because of a historical bug that cannot be fixed.
let userName = null;
console.log(typeof(userName)); // object
So how can you now check for null? This article will teach you how to check for null, along with the difference between the JavaScript type null and undefined. The libraries can be used privately in your workspace or shared with other projects or Angular Course in Pune developers by being published as npm packages.
Null vs Undefined in JavaScript
Null and undefined are very similar in JavaScript and are both primitive types.
A variable has the type of null if it intentionally contains the value of null. In contrast, a variable has the type of undefined when you declare it without initiating a value.
// This is null
let firstName = null;
// This is undefined
let lastName;
Undefined works well because when you check the type using the typeof() method, it will return undefined:
let lastName;
console.log(typeof(lastName)); // undefined
For Free Demo classes Call: 020-71173125
Registration Link: Click Here!
How to Check for Null in JavaScript with Equality Operators
The equality operators provide the best way to check for null. You can either use the loose/double equality operator (==) or the strict/triple equality operator (===).
How to use the loose equality operator to check for null
You can use the loose equality operator to check for null values:
let firstName = null;
console.log(firstName == null); // true
But, this can be tricky because if the variable is undefined, it will also return true because both null and undefined are loosely equal. Elevate Your Web Development Skills with Our React JS Course in Pune – Enroll Now for Expert-Led Training!
let firstName = null;
let lastName;
console.log(firstName == null); // true
console.log(lastName == null); // true
console.log(firstName == undefined); // true
console.log(lastName == undefined); // true
console.log(firstName == lastName); // true
console.log(null == undefined); // true
Now, if you only want to check for null – then you can use the strict equality operator.
For Free Demo classes Call: 020-71173125
Registration Link: Click Here!
How to use the strict equality operator to check for null
The strict equality operator, compared to the loose equality operator, will only return true when you have exactly a null value. Otherwise, it will return false (this includes undefined).
let firstName = null;
let lastName;
console.log(null == undefined); // true
console.log(null === undefined); // false
console.log(firstName == lastName); // true
console.log(firstName === lastName); // false
console.log(firstName === null); // true
console.log(firstName === undefined); // false
console.log(lastName === null); // false
console.log(lastName === undefined); // true
As you can see, it only returns true when a null variable is compared with null, and an undefined variable is compared with undefined.
How to Check for Null in JavaScript with the Object.is() Method
Object.is() is an ES6 method that determines whether two values are the same. This works like the strict equality operator.
// Syntax
Object.is(value1, value2)
For Free Demo classes Call: 020-71173125
Registration Link: Click Here!
Let’s make use of the previous example to see if it works like the strict equality operator:
let firstName = null;
let lastName;
console.log(Object.is(firstName, lastName)); // false
console.log(Object.is(null, undefined)); // false
console.log(Object.is(firstName, null)); // true
console.log(Object.is(firstName, undefined)); // false
console.log(Object.is(lastName, null)); // false
console.log(Object.is(lastName, undefined)); // true
Do watch our Components Of Angular video on YouTube.
This happens because it only returns true when both values are the same. This means that it will only return true when a variable set to null is compared with null, and an undefined variable is compared with undefined.
Author:-
Mayuresh Marale
Call the Trainer and Book your free demo Class For Javascript
Call now!!! | SevenMentor Pvt Ltd.
© Copyright 2021 | SevenMentor Pvt Ltd.