ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**转换成数值类型:** 1. Number() 2. parseInt() **两种方法转换结果对比:** | 示例 | Number() | parseInt | | --- | --- | --- | | '123' | 123 | 123 | | 'abc' | NaN | NaN | | false | 0 | NaN | | 'abc789' | NaN | NaN | | '123abc456' | NaN | 123 | 示例: ``` var s1 = '123'; var s2 = true; var s3 = 'abc456'; var s4 = '123def789'; var s5 = 'def789aaa852'; console.log('-------Number()--------'); // Number() 方法 console.log(Number(s1)); //输出:123 console.log(Number(s2)); //输出:1 console.log(Number(s3)); //输出:NaN console.log(Number(s4)); //输出:NaN console.log('-------parseInt()--------'); //parseInt() 方法 console.log(parseInt(s1)); //输出:123 console.log(parseInt(s2)); //输出:NaN console.log(parseInt(s3)); //输出:NaN console.log(parseInt(s4)); //输出:123 ``` ![](https://img.kancloud.cn/36/da/36da340c078f3b4b1d9372c21dc68bd6_650x264.png)