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

2013 02 21 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


typeof +""; // number

// even
typeof -""; // "number"

// BUT!!!
-"foo"; //NaN
typeof -"foo"; // "number"

// because
typeof NaN === 'number'; // Despite being "Not-A-Number"

kirillov-artur

Fork me on GitHub