I found ==
is little confusing for newbies, So I want someone to explain how it works.
For example -
new String("a") == "a" and "a" == new String("a") are both true.
new String("a") == new String("a") is false.
Why?
I found ==
is little confusing for newbies, So I want someone to explain how it works.
For example -
new String("a") == "a" and "a" == new String("a") are both true.
new String("a") == new String("a") is false.
Why?
==
is called comparison/equality operator, it compares 2 values, but not their data types so for example
1 == '1'
will return true
, for stricter comparison, use ===
which will compare the data types too so 1 === '1'
will return false
==
is a comparison operator that means "equal to" but does not take variable typing into account.
===
is a stricter comparison operator that means "equal to and same type".
So if you have a string
called numberStr
with a value of 2
and an integer
called numberInt
with a value of 2
, they will evaluate as follows:
numberStr == numberInt // evaluates to true
numberStr === numberInt // evaluates to false because types are different