We have come to love and respect the fact that (almost) everything is an object in Javascript and we can use all kinds of variables without giving a damn about their types.
But, we also can understand that Javascript is trying to work its magic when we throw all caution to the wind. This may have adverse effects.
Consider using Booleans as an example.
|
|
The !!
just gives the right boolean
value - it is equivalent to the variable being used in an if
condition.
Now, have a look at what happens if we use a Boolean
object.
|
|
It makes absolute sense if you think about it. falseObj
is -
- an object that exists and
- the above expression evaluates to
true
because of the object’s mere existence
This is equivalent to -
|
|
What I do to avoid the potential catastrophe is to avoid using Boolean
object altogether. Let’s be frank here - you may not even have noticed that such an object exists for all practical purposes.