目录:
JavaScript——数据类型转换(Number)
JavaScript——数据类型转换(String)
JavaScript——数据类型转换(Boolean)
我们在进行前端JavaScript开发中,经常遇到需要判断数据类型和数据类型转换的需求,其中又分隐式和显式。我们解决一下~~~
数据类型的判断
- 对于原始类型,用typeof即可
- 对于引用类型,大多数用instanceof
转换为数字
隐式类型转换
var num=5;
var str='2';
console.log('数字-隐式类型转换',typeof(num-str),num-str);
输出 数字-隐式类型转换 number 3
+号 一般不是把字符串转换为数字,而是把数字转换为字符串
console.log('数字-隐式类型转换',typeof(num+str),num+str);
输出 数字-隐式类型转换 str 52
显式类型转换
库函数:Number();把一个能转换为数字的其它类型转化为数字,
var tmp1= '2';
var tmp= Number(tmp1);
console.log('数字-显示类型转换',typeof(tmp),tmp)
输出 数字-显示类型转换 number 2
var tmp1='1.2E5';
var tmp=Number(tmp1);
console.log('数字-显示类型转换',typeof(tmp),tmp);
输出 数字-显示类型转换 number 120000
NaN not a number
var tmp1='xyz';
var tmp= Number(tmp1);
console.log('数字-显示类型转换',typeof(tmp),tmp)
输出 数字-显示类型转换 number NaN
var tmp1=true;
var tmp=Number(tmp1);
console.log('数字-显示类型转换',typeof(tmp),tmp);
输出 数字-显示类型转换 number 1
var tmp1=''
var tmp=Number(tmp1)
console.log('数字-显示类型转换',typeof(tmp),tmp)
输出 数字-显示类型转换 number 0
null==0 不等,但是,Number(null)==0 相等
var tmp1=null;
var tmp= Number(tmp1);
console.log('数字-显示类型转换',typeof(tmp),tmp)
输出 数字-显示类型转换 number 2
var tmp1=undefined;
var tmp=Number(tmp1);
console.log('数字-显示类型转换',typeof(tmp),tmp)
输出 数字-显示类型转换 number NaN
库函数 parseInt(),把一个字符串按照整数的方式"解析",如果只有开头一部分可以,解析一部分;如果解析不了,NaN
var tmp1='1234';
var tmp=parseInt(tmp1);
console.log('1234',tmp);
输出 1234 1234
var tmp1='100px';
var tmp=parseInt(tmp1);
console.log('100px',tmp);
输出 100px 100
var tmp1='a100';
var tmp=parseInt(tmp1);
console.log('a100',tmp);
输出 a100 NaN
var tmp1='';
var tmp=parseInt(tmp1);
console.log('空字符串',tmp);
输出 空字符串 NaN
十六进制
var tmp1='0xA0'; //A x 16 +0=160
var tmp=parseInt(tmp1)
console.log('0xA0',tmp);
输出 0xA0 160
var tmp1='A0';
var tmp=parseInt(tmp1,16);
console.log('A0',tmp);
输出 A0 160
parseInt()当做取整来用
var tmp1='1.5';
var tmp=parseInt(tmp1);
console.log('1.5',tmp)
输出 1.5 1
var tmp1='1.2E5';
var tmp=parseInt(tmp1);
console.log('1.2E5',tmp)
输出 1.2E5 1
undefined null false,true
var tmp1=undefined;
var tmp=parseInt(tmp1);
console.log('undefined',tmp)
输出 undefined NaN
var tmp1=null;
var tmp=parseInt(tmp1);
console.log('null',tmp);
输出 null NaN
var tmp1=false;
var tmp=parseInt(tmp1);
console.log('false',tmp);
输出 false NaN
如果记忆Number()和parseInt()?
Number():本质上是否能转换为数字,
parseInt():开头的部分看上去更像数字,
哪些是Number()可以,parseInt()不可以:false,null...,
哪些是Number()不可以,parseInt()可以:数字开头的字符串,
Number()和parseInt()都不可以:undefined,字母开头的字符串
库函数:parseFloat():主要用于浮点数和科学计数法
var tmp1='10.00';
var tmp=parseFloat(tmp1);
console.log('10.00',tmp)
输出 10.00 10
var tmp1='1.2E5';
var tmp=parseFloat(tmp1);
console.log('1.2E5',tmp)
输出 1.2E5 120000
var tmp1='10.00aaaa';
var tmp=parseFloat(tmp1);
console.log('10.00aaaa',tmp)
输出 10.00aaaa 10
isNaN(),如果不是数,那么会自动调用Number()
console.log('NaN',isNaN(NaN));
console.log('a100',isNaN('a100'));
console.log('undefined',isNaN(undefined));
console.log('null',isNaN(null));
console.log('1234',isNaN('1234'));
console.log('空字符串',isNaN(''));
输出:
NaN true
a100 true
undefined true
null false
1234 false
空字符串 false
关注下面微信公众号,可以领取微信小程序、Vue、TypeScript、前端、uni-app、全栈、Nodejs、Python等实战学习资料