/** * 转换成数字,有两种方法: * 1.Number(var) * 2.parseInt(var)与parseFloat(var) */// 字符串转换成数字// 纯字母var foo = ‘hello‘;console.log(`Number(foo): ${Number(foo)}`); // Number(foo): NaN// 字母数字混合var foo = ‘hello123‘;console.log(`Number(foo): ${Number(foo)}`); // Number(foo): NaN// 纯数字var foo = ‘8848‘;console.log(`Number(foo): ${Number(foo)}`); // Number(foo): 8848var foo = ‘3.14234‘;console.log(`Number(foo): ${Number(foo)}`); // Number(foo): 3.14234// booleanvar foo = true;console.log(`Number(foo): ${Number(foo)}`); // Number(foo): 1var foo = false;console.log(`Number(foo): ${Number(foo)}`); // Number(foo): 0// nullvar foo = null;console.log(`Number(foo): ${Number(foo)}`); // Number(foo): 0// undefinedvar foo = undefined;console.log(`typeof foo: ${typeof foo}, Number(foo): ${Number(foo)}`); // typeof foo: undefined, Number(foo): NaN// 字母数字混合// 字母在前var foo = ‘hello123‘;console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): NaN// 字母在后var foo = ‘123hello‘;console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): 123// 穿插var foo = ‘13word23hello‘;console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): 13var foo = ‘w13ord23hello‘;console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): NaN// 夹带浮点数var foo = ‘hello23.54‘;console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): NaNvar foo = ‘23.54hello‘;console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): 23var foo = ‘0.999hello‘;console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): 0var foo = ‘wor0.999hello‘;console.log(`parseInt(foo): ${parseInt(foo)}`); // parseInt(foo): NaN