DEV Community

RanveerSequeira
RanveerSequeira

Posted on

== VS === in JavaScript

Facts -

  • String
  • Number
  • Boolean
  • Undefined
  • Null

These are called primitive datatype in JavaScript. Except these everything else is an OBJECT.

Class, Function, Array, RegExp all are ObJect.

Objects are passed by reference, whereas primitives are passed by values.

Examples

  1. 'ranveer' === new String('ranveer') //false
  2. 'ranveer' === new String('ranveer).toString() // true
  3. 'ranveer' == new String('ranveer') // true

We are getting this strange outputs because in javascript

(==) do automatic typecasting,whereas (===) does not.

On line 1, LHS is String and RHS is Object. Hence, its False.

On line 2, LHs is String and RHS is String (we typecaste it toString). Hence, its True.

On line 3, LHS is String and RHS is Object. But (==) is smart kid he performs automatic typecasting. He converted RHS object into string. Hence, its True

Top comments (0)