Before going to the examples, let’s see what is type coercion.
What is Type Coercion?
Type coercion can lead to unexpected values in your code and also cause errors.
Therefore, whenever we check if two values are equal, it is better to use strict equals operators === and !== rather than == and != as these strict operators check that the value and data types match.
Why Type Coercion occurs?
=== vs ==
To understand type coercion better, we must have good knowledge of == and === operator.
Is Equal To (==)
This operator compares two values (numbers, strings, or Boolean’s) to see if they are the same.
'Passion' == 'Programming' returns false; Because they are not the same string. 'Passion' == 'Passion' returns true; Because they are the same string.
It is usually preferred to use the strict method.
Strict Equal To (===)
This operator compares two values to check that both the data type and value are the same.
Is Not Equal To ( !=)
This operator compares two values (numbers, strings, or Boolean’s) to see if they are not the same.
'Hi' != 'Hello' returns true Because they are not the same string 'Hi' != 'Hi' returns false Because they are the same string
Strict Not Equal To ( !==)
This operator compares two values to check that both the data type and value are not the same.
'5' !== 5 returns true Because they are not the same data type '5' != '5' returns false Because they are the same data type and value
Because of type coercion, the strict equality operators === and !== result in fewer unexpected values rather than == and != do.
When we use == the following values can be considered equal: false, 0 and ‘ ‘ (empty string). However, they are not equal when we use the strict operators.
Although null and undefined are both falsy, they are not equal to anything other than themselves. Again, they are not equivalent when we use strict operators.
Although NaN is considered falsy, it is not equal to anything. It is not even equivalent to itself. Since NaN is an undefinable number, two NaN cannot be equal.
let value = 7 + '2'; console.log(value); //72
What happens when we give, 3 + 4 + ‘3’?
let value = 3 + 4 + '3'; console.log(value); //73
Let’s see some more examples to understand better,
With Boolean values, again the same concept, if i want to convert a boolean to a number is very easy.
true means literal 1 false meas literal 0
let value = 3 + true; console.log(value); //4
Let’s make it little bit complex,
let value = 3 + true + '4'; console.log(value); //44
Boolean and string
let value = true + '4'; console.log(value); //true4
What happen if I try to convert string to a number?
let value1 = Number('Mei'); console.log(value1); //NaN let value2 = Number('2'); console.log(value2); //2
Number to Boolean
let value1 = Boolean(1); //value1 = true; let value2 = Boolean(0); //value2 = false;
What happen, If I convert number 6 to boolean?
let value = Boolean(6); //value = true;
In the above example, we got true. Because, while converting number to boolean, only one number will return false, that is 0.
Other than 0, all numbers will return true.
String to Boolean
let value = Boolean('mei'); //value = true let value1 = Boolean('10000'); //value1 = true let value2 = Boolean(''); //value2 = false;
All the string returns true when converting string to boolean, except empty string.
When we convert empty string to boolean, it returns false.
undefined to Boolean
let value = Boolean(undefined); //value = false;
null to Boolean
let value = Boolean(null); //value = false
So null, undefined, ‘ ‘ (empty string), 0 gets false.
Based on the above information, consider the below example, how I’m going to improve my code.
Thanks for reading my article, if you found this information’s useful, please follow by blog, it will really motivates me to write more informative articles like this.