Why does !{}.length
evaluate to true
in JavaScript if {}.length
throws the error Uncaught SyntaxError: Unexpected token '.'
. In fact, !{}.definitelyNotAMethod
also evaluates to true
.
Objects don't possess length methods, but if you were expecting the object to be an array and didn't perform the proper validations, this could cause a hard-to-spot bug in your code base.
Thanks for taking a look!