Table of contents
"What's different, it's the same yes?? π€π"
No learner, it's not the same as you think by just adding one more equal to sign (=
).
FIRSTLY,
\== operator is equal to "Loose Equality"
\=== operator is equal to "Strict Equality"
\== Operator (Loose Equality)
let's see the example
const a = 5;
const b = "5";
console.log(a == b); //output: true
what are you trying to say Vatsal?
I am trying to say that if we have taken a variable with the name a
and assigned a number
as 5
and another variable with the name b
and assigned with a string
as 5
.
So in the 1st case, it will convert the number
into a string
. (Type Conversion)
so, Now if we compare two with (==) we will get true
as an output.
There are some cool rules :
If either operand is a
string
, the other operand will be converted to astring
.If either operand is a
number
, the other operand will be converted to anumber
If either operand is a
boolean
, it will be converted to anumber
(true
becomes1
andfalse
becomes0
).If one operand is an
object
and the other is a primitive value, the object will be converted to a primitive value before the comparison is made.If one of the operands is
null
orundefined
, the other must also benull
orundefined
to return true. Otherwise, it will returnfalse
.
\=== Operator (Strict Equality)
Let's see with an example
const a = 5;
const b = "5";
console.log(a === b); //output: false
So, here in ===
operator, there is no type conversion. And it's just checking the value.
i.e we have taken a variable with the name a
and assigned a number as 5,
and another variable with a name b
and assigned a string as 5
and over here one is a number and another one is in string. Both things can't be equal and the output will be false.
You can follow me over hereπ±:
Linkedin: https://www.linkedin.com/in/vatsal-singh-732a20191/