2

I don't understand it. When would you use the boolean object?

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
DarkLightA
  • 14,980
  • 18
  • 49
  • 57

1 Answers1

2

There's really no reason to ever use the object directly, just use boolean literals or type-coerce other values to boolean (!something or !!something). A boolean object invoked as new Boolean(false) will evaluate to true, because an object is "truthy".

eyelidlessness
  • 62,413
  • 11
  • 90
  • 94
  • 1
    Wow, interesting to know. But makes yo think why would anyone put this useless of an object into a language. – Jonathon May 26 '11 at 02:37
  • @Jonathon, it's not so much that it's useless, just that it behaves differently than (intuitively) expected. Its use would be to access the boolean's value as an object property (eg `var f = new Boolean(false); f.valueOf(); // false`), which can allow duck-typing (eg. accepting other object types which provide `valueOf` method that returns a boolean value). So it's probably not fair for me to have said there's "no reason to ever use the object directly". That said, it's also in the language because it provides the basis for the boolean literal (just as `Object` provides the basis for `{}`). – eyelidlessness May 26 '11 at 06:45