While learning about the optional chaining operator I experimented a bit and found out that these two evaluate to undefined:
false.nonExistingProperty // undefined
true.nonExistingProperty // undefined
But why is that so? Shouldn't this throw an error?
When properties are accessed on primitives, JavaScript auto-boxes the value into a wrapper object and accesses the property on that object instead.
https://developer.mozilla.org/en-US/docs/Glossary/Primitive
false and true are implicitly converted to Boolean objects here, i.e. you are reading nonExistingProperty from a Boolean instead of the boolean primitive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With