I am reading through the Mozilla Manual on JavaScript, and I come to this point in my reading, Boolean object. I can’t see a single use for them. What’s their point? Why wouldn’t you use just true and false?
By the way, I don’t know Java at all and I’m not afraid of learning new things that consequently why I’m trying to learn JavaScript. I’m a PHP programmer, a back end guy, and I’d like to learn how to do some front end work, so I’m reading the Mozilla JavaScript Guide.
Because this is (somewhat sadly) how the language was defined — I suspect it was originally for performance/optimization; note the case of assignment to a
stringproperty below. (Java works similarly, although Scala and Python largely reject this distinction).Note that
Booleanisn’t the only “wrapper type”. There are alsoStringandNumber, for instance.Because of this there remains a number of quirks (the below could just as much apply to Boolean):
I know this didn’t “answer” the question, but rather chucks some more wood on the fire 😉
The only real “gotcha” otherwise from the above is that perhaps
new Boolean(false)is a truth-y value.Happy coding.