Deprecating the Boolean object

The problem is that Boolean is (afaik) the only type where Type(new Type(x)) !== Type(x).

For example String(new String(10)) === '10' but Boolean(new Boolean(false)) === true.

It's absurd to have to access the value of the object in its own "type cast" or in an if statement for example.

I understand the reason this happens is that evaluating the "truthiness" of an object will yield true but this should be a special case, how come document.all gets to be a falsy "object" but a Boolean doesn't?

Also, there is no need for a Boolean object wrapper at all,

Boolean(x) === !!x
new Boolean(x) β‰ˆβ‰ˆβ‰ˆ { valueOf = () => !!x }

If for some reason someone needs a Boolean wrapper or to explicitly convert to boolean it's really simple to implement.

document.all was required because too many websites were using a bad pattern and they couldn't all be updated. For non-web hosts like Node, Deno, Bun, XS, they don't have document.all so they have the untainted model where "every object is truthy, no exceptions".

OK, but it still doesn't justify having a Boolean object, it can just make things more complicated and maybe even confuse new developers...

I agree, Boolean objects should be avoided. It is unlikely that new Boolean can be removed from the language now as that would be a breaking change. Linting rules and teaching materials should discourage its use in new code.

Here's the long and short of it: ToBoolean does not depend on Symbol.toPrimitive, valueOf, or toString, largely for performance reasons.

All other primitives do depend on that, hence the difference.

Boolean is useless without valueOf as it's always true so it's really unnecessary to have it...

None the less, it exists and must forever exist for backwards compatibility.

1 Like

Yeah, I'm not suggesting removing it completely only deprecating it

Define "deprecated"? If by that term you simply mean "discourage it's use", I think that's already being done?

Well, I figured Mozilla's mdn is the "official javascript documentation" and there the Boolean object isn't marked as deprecated.

Because it’s not harmful to use it, so it shouldn’t be.

Boolean() is good and fine, it’s new Boolean() that should be avoided.

1 Like

@donno2048 MDN is not "official" as in, it's not "blessed by TC39". Our (MDN's) deprecation status faithfully reflects the spec and we don't make normative statements like deprecation ourselves. It would be TC39's job to mark Boolean objects as "deprecated", which I don't think they would do (the spec doesn't deprecate things just because it's hard to use or contains footguns; only dangerous or things never intended to exist can be). OTOH, the constructor page: Boolean() constructor - JavaScript | MDN already has a mention that " You should rarely find yourself using Boolean as a constructor." which to me is a sufficient callout to developers. I do have plans, though, to update all primitive wrapper objects' landing pages so they start with a note like "you should almost never work with the wrapper objects".

5 Likes