4 ms·
Does JavaScript not have something like "bool(value)"?
by milliams 13y ago
Does JavaScript not have something like "bool(value)"?
- benjoffe 13y agoYou can use Boolean(value) but it will return an 'object' of a 'boolean' type, rather than a primitive boolean (it's one of JS's flaws so is generally avoided). Note: their example of a similar piece of code is not identical, as it won't cast values like null to false. I think the best example of a clear equivalent would be: value = value ? true : false;
- gsnedders 13y ago`Boolean(value)` returns a boolean primitive; `new Boolean(value)` returns a boolean object (which you never want). Compare: > typeof Boolean(1) 'boolean' > typeof new Boolean(1) 'object'
- Guillaume86 13y agoIn chrome console I get an object for new Boolean(false) but I think it is the primitive for Boolean(false), am I right or I missed something?
- deleted 13y ago[deleted]
- onestone 13y agoThe !! mentioned in the article does exactly that.