behaviour of typeof in javascript - Stack Overflow

Can someone explain why typeof behaves in the following way:typeof Returns: SyntaxError: Unexpected

Can someone explain why typeof behaves in the following way:

typeof 
//Returns: SyntaxError: Unexpected token } (Quite obvious)

"Why am I a " + typeof 
//Returns: SyntaxError: Unexpected token }

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

"Why am I a " + typeof + "??"; 
//Returns: "Why am I a number"

Can someone explain why typeof behaves in the following way:

typeof 
//Returns: SyntaxError: Unexpected token } (Quite obvious)

"Why am I a " + typeof 
//Returns: SyntaxError: Unexpected token }

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

"Why am I a " + typeof + "??"; 
//Returns: "Why am I a number"
Share Improve this question asked Mar 29, 2013 at 10:30 gopi1410gopi1410 6,62510 gold badges43 silver badges76 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

typeof isn't a function but a unary operator, so

typeof + ""; 

is the same as

typeof (+ "");

and +something converts the something to a number as is precised in EcmaScript norm on the unary + operator :

The unary + operator converts its operand to Number type.

+"..." will actually parse the string as a number. This will result in typeof + "" returning "number", even though the returned number is NaN.

The first two usages are simply wrong, since typeof needs a right hand side.

References:

  • typeof operator
  • Unary + operator

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742415323a4439630.html

相关推荐

  • behaviour of typeof in javascript - Stack Overflow

    Can someone explain why typeof behaves in the following way:typeof Returns: SyntaxError: Unexpected

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信