-8

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?

Moazzam Khan
  • 3,130
  • 2
  • 20
  • 35

2 Answers2

1

== 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

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
0

== 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
Ennui
  • 10,102
  • 3
  • 35
  • 42