JavaScript的数据类型转换(显式and隐式转换)

JavaScript的数据类型转换(显式and隐式转换)

转换为字符串类型

显示转换

String() 方法 : 将数据转换为字符串类型

使用:String(变量名)

例子:

            let num = 123
            let boo = true
            let unde = undefined
            let nuls = null
            let arr = [1]
            let object = {
                num: 123
            }
            console.log(String(num),typeof String(num)); 
            console.log(String(boo),typeof String(boo));
            console.log(String(unde),typeof String(unde));
            console.log(String(nuls),typeof String(nuls));
            console.log(String(arr),typeof String(arr));
            console.log(String(object),typeof String(object));

打印展示:
请添加图片描述

隐式转换

使用:变量名 + ‘ ’。即字符串拼接

例子:

            let str = '123'
            let num = 123
            let boo = true
            let unde = undefined
            let nuls = null
            let arr = [1]
            let object = {
                num: 123
            }
            console.log(num+str,typeof (num+str));
            console.log(boo+str,typeof (boo+str));
            console.log(unde+str,typeof (unde+str));
            console.log(nuls+str,typeof (nuls+str));
            console.log(arr+str,typeof (arr+str));
            console.log(object+str,typeof (object+str));

打印展示:
请添加图片描述

转换为数字类型

显示转换

1、Number() 方法 : 将数据转换为数字类型

使用:Number(变量名)

如果该值无法转换为合法数字,则返回 NaN。

看起来像数字的就能转换成数字类型。

特殊:(可以转换为数字类型的)

  1. “Infinity":表示无穷的
  2. 1e12:科学计数法
  3. 0xaa:十六进制。二进制、八进制等都可以

例子:

            let str = '123'
            let num = 123
            let boo = true
            let unde = undefined
            let nuls = null
            let arr = [1,'str']
            let arr1 = [1]
            let object = {
                num: 123
            }
            let inf = 'Infinity'
            let scient = '1e12'
            let binary = '0xaa'

            console.log(Number(num),typeof Number(num));
            console.log(Number(boo),typeof Number(boo));
            console.log(Number(unde),typeof Number(unde));
            console.log(Number(nuls),typeof Number(nuls));
            console.log(Number(arr),typeof Number(arr));
            console.log(Number(arr1),typeof Number(arr1));
            console.log(Number(object),typeof Number(object));
            console.log(Number(inf),typeof Number(inf));
            console.log(Number(scient),typeof Number(scient));
            console.log(Number(binary),typeof Number(binary));

打印展示:
请添加图片描述

2、parseInt()方法:可解析一个字符串,并返回一个整数。

注意

  1. 只有字符串中的第一个是数字才会被解析返回。
  2. 开头和结尾的空格是允许的。
  3. 中间遇到不是数字的不继续解析后面还是数字的了
  4. 如果字符串的第一个字符不能被转换为数字,那么 parseInt() 会返回 NaN。

3、parseFloat:可解析一个字符串,并返回一个浮点数。

与parseInt()差不多,只是一个解析后返回整数,一个返回浮点数。注意点也是类似的【就不再说明了】

两个合起来,例子

            let num1 = '10.325'
            let num2 = '10.2mm12'
            let boo1 = null
            console.log(parseInt(num1),typeof parseInt(num1));
            console.log(parseInt(num2),typeof parseInt(num2));
            console.log(parseInt(boo1),typeof parseInt(boo1));
            console.log(parseFloat(num1),typeof parseFloat(num1));
            console.log(parseFloat(num2),typeof parseFloat(num2));
            console.log(parseFloat(boo1),typeof parseFloat(boo1));

打印展示:
请添加图片描述

隐式转换

数据间进行加减乘除运算可以隐式转换

但是 加法运算的时候遇上字符串类型,优先转换成字符串

            let str = '123'
            let num = 123
            let boo = true
            let unde = undefined
            let nuls = null
            let arr = [1,'str']
            let object = {
                num: 123
            }
            console.log(str+num,typeof (str+num),"分割",str-num,typeof (str-num),"分割",str*num,typeof (str*num),"分割",str/num,typeof (str/num));
            console.log(boo+num,typeof (boo+num),"分割",boo-num,typeof (boo-num),"分割",boo*num,typeof (boo*num),"分割",boo/num,typeof (boo/num));
            console.log(unde+num,typeof (unde+num),"分割",unde-num,typeof (unde-num),"分割",unde*num,typeof (unde*num),"分割",unde/num,typeof (unde/num));
            console.log(nuls+num,typeof (nuls+num),"分割",nuls-num,typeof (nuls-num),"分割",nuls*num,typeof (nuls*num),"分割",nuls/num,typeof (nuls/num));
            console.log(arr+num,typeof (arr+num),"分割",arr-num,typeof (arr-num),"分割",arr*num,typeof (arr*num),"分割",arr/num,typeof (arr/num));
            console.log(object+num,typeof (object+num),"分割",object-num,typeof (object-num),"分割",object*num,typeof (object*num),"分割",object/num,typeof (object/num));

打印展示:

请添加图片描述

转换为浮点类型

显示转换

Boolean() 方法 : 将数据转换为字符串类型

使用:Boolean(变量名)

            let str = '123'
            let num = 123
            let boo = true
            let unde = undefined
            let nuls = null
            let arr = [1,'str']
            let object = {
                num: 123
            }
            console.log(Boolean(num),typeof Boolean(num));
            console.log(Boolean(boo),typeof Boolean(boo));
            console.log(Boolean(unde),typeof Boolean(unde));
            console.log(Boolean(nuls),typeof Boolean(nuls));
            console.log(Boolean(arr),typeof Boolean(arr));
            console.log(Boolean(object),typeof Boolean(object));

打印展示:
在这里插入图片描述

隐式转换

规则:遇到(六大假值):"",0 ,undefined ,null, NaN, false:就为假

其余都为真

            let boo10 = '';
            let boo2 = '100';
            let boo3 = 0;
            let boo4 = 1;
            let boo5 = null;
            let boo6 = undefined;
            let boo7 = NaN;
            let boo8 = ['123']
            let boo9 = {
                num : '111'
            }
            console.log(!!boo10,typeof (!!boo10));
            console.log(!!boo2,typeof (!!boo2));
            console.log(!!boo3,typeof (!!boo3));
            console.log(!!boo4,typeof (!!boo4));
            console.log(!!boo5,typeof (!!boo5));
            console.log(!!boo6,typeof (!!boo6));
            console.log(!!boo7,typeof (!!boo7));
            console.log(!!boo8,typeof (!!boo8));
            console.log(!!boo9,typeof (!!boo9));

打印展示:
请添加图片描述

后续有更多发现再补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值