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
- 'ranveer' === new String('ranveer') //false
- 'ranveer' === new String('ranveer).toString() // true
- '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)