console.log(+‘2’) console.log(-‘2’) 输出什么?

+‘2’ 会将字符串‘2’转换为number类型2

-‘2’会将字符串‘2’转换为number类型1(自减);

所以

数字字符串之前存在数字中的正负号(+/-)时,会被转换成数字

console.log(typeof '3'); // string
console.log(typeof +'3'); //number

同样,可以在数字前添加 '',将数字转为字符串

console.log(typeof 3); // number
console.log(typeof (''+3)); //string

对于运算结果不能转换成数字的,将返回 NaN

console.log('a' * 'sd'); //NaN
console.log('A' - 'B'); // NaN

  

posted @ 2016-06-26 20:57  Y_WEB  阅读(1631)  评论(0编辑  收藏  举报