除了在算术函数和表达式中,会自动进行 number 类型转换之外,可以使用Number(Value)进行显式的转换。
数字类型转换规则
类型 | 类型转换后 |
undefined | NaN |
null | 0 |
true | 1 |
false | 0 |
string | 去掉首尾空格后的纯数字字符串中含有的数字。如果剩余字符串为空,则转换结果为 |
例子:
let val1 = '12567';
let val2 = "233.9898";
let val3 = null;
let val4 = ' 125 ';
let val5 = 'nihao';
let val6 = true;
let val7 = false;
console.log( "6" / "2")
console.log( typeof "6" / "2")
console.log( typeof ("6" / "2"))
console.log('val1:',typeof val1)
console.log(Number(val1))
console.log(typeof Number(val1))
console.log('val2:',typeof val2)
console.log(Number(val2))
console.log(typeof Number(val2))
console.log('val3:',typeof val3)
console.log(Number(val3))
console.log(typeof Number(val3))
console.log('val4:',typeof val4)
console.log(Number(val4))
console.log(typeof Number(val4))
console.log('val5:',typeof val5)
console.log(Number(val5))
console.log(typeof Number(val5))
console.log('val6:',typeof val6)
console.log(Number(val6))
console.log(typeof Number(val6))
console.log('val7:',typeof val7)
console.log(Number(val7))
console.log(typeof Number(val7))
结果:
标签:类型转换,console,数字,JavaScript,Number,let,typeof,log From: https://blog.51cto.com/u_16830897/12091591