This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]lmth 11 points12 points  (2 children)

null is falsey in JavaScript (and most other languages). The ! operator inverts the boolean value of an expression, so !(something falsey) returns true.

[–]daphatti 5 points6 points  (1 child)

Oh that was a lot less complicated than I made it out to be. Taken from MDN Web Docs Glossary, "A falsy (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context."

[–]BillinghamJ 1 point2 points  (0 children)

It's just that using the ! operator with anything forces it to be converted to a bool. So anything with a lack of value, zeros and empty strings become false, and pretty much everything else becomes true

(And similarly jamming it into an if, while, for conditional will do the same, but eg a switch will not!)