created by Brian Leroux & Andrew Lunny. sparodically uncurated by David Trejo.

why am i a number

Can someone tell me?

    "Why am I a " + typeof + ""; // "Why am I a number"

@jhnnns


I think I can tell you.

typeof requires some value or expression that is evaluated and passed then to typeof as an argument. +"" is treated as expression because + operator can be used as an unary operator (syntax is: + UnaryExpression). + converts its operand to Number type. So, + "" is evaluated to 0 and then 0 is passed to typeof.

Note that we can cast strings to numbers by preceding them with +:


(2 + "3");  // 23
(2 + +"3"); // 5
(+"");      // 0

@wojciechfornal

Fork me on GitHub