4 ms·
The output is "weird" if you don't know the rules for operator precedence and how things convert to their primitive values. Most people arent going to "know" w
by zdragnar 5y ago
The output is "weird" if you don't know the rules for operator precedence and how things convert to their primitive values.
Most people arent going to "know" what '+!![]' will resolve to because it makes literally no sense to combine those operators into a single expression in anything approaching normal code.
- zarzavat 5y agoI have definitely used the +!! "operator" before. It coerces a value into a boolean integer. It's not weird at all, just a mechanical application of the not ! and Number coercion operators +. The fact that [] is truthy is something everybody learns in their first weeks of JS programming otherwise you would be writing `if (someArray)` and wondering why your code is broken. A weird one would be to explain why +[] is 0 and +{} is NaN. That is nonsensical.
- denton-scratch 5y agoThis is true; a lot of the questions are about automatic type conversions.