JavaScript数据类型转换

类型转换:

             - 运算符和函数会自动将赋予它们的值转换为正确的类型

1、字符串转换

                - 将其他类型转换为string, 调用String(变量)函数

                - 函数的返回值,重新赋值给原来的变量,改变原变量的数据类型

                - alert(value);将value转换为字符串类型,然后显示

字符串转换
     let res = true;
     console.log(typeof res); //boolean
     console.log(res); //true


    //将变量作为参数传递给string函数,并将返回值赋值给res
     res = String(res);
     console.log(res2); //"true"
     console.log(typeof res); //string


     res = 10;
     console.log(typeof res, res); //number 10
     res = String(res);
     console.log(typeof res, res); //string , "10"


     res = null;
     console.log(typeof res, res); //Object  null
     res = String(res);
     console.log(typeof res, res); //string, "null"

2、数字转换

                - 将其它类型转换为Nubmer类型,调用Number()函数

                undefined   NaN

                null        0

                true/false  1/0

                string    

                    1、 纯数字字符串,可以转换

                    2、 数字字符串,前后带空格,可以转换。

                    3、 数字字符串,中间带空格,不能转换,返回NaN

                    4、 带字母的字符串等其他,转换为NaN

                    5、 ""和" " 转换为0

//数字转换
        //数字类型字符串
        // let str = "123";
        // console.log(str, typeof str); //"123" string
        // str = Number(str);
        // console.log(str, typeof str); //123 number

        // //数字字符串 + 前后添加空格
        // str = " 123 "
        // console.log(str, typeof str); //" 123 " string
        // str = Number(str);
        // console.log(str, typeof str); //123 number

        // //中间带空格
        // str = " 1 23 "
        // console.log(str, typeof str); //" 1 23 " string
        // str = Number(str);
        // console.log(str, typeof str); //NaN number


        // //带字母
        // str = " 123a "
        // console.log(str, typeof str); //" 123a " string
        // str = Number(str);
        // console.log(str, typeof str); //NaN number

        // str = "abc"
        // console.log(str, typeof str); //"abc" string
        // str = Number(str);
        // console.log(str, typeof str); //NaN number


        // str = ""
        // console.log(str, typeof str); //"" string
        // str = Number(str);
        // console.log(str, typeof str); //0 number

        // str = " "
        // console.log(str, typeof str); //" " string
        // str = Number(str);
        // console.log(str, typeof str); //0 number


        // str = null;
        // console.log(str, typeof str); //null Object
        // str = Number(str);
        // console.log(str, typeof str); //0 number

        // str = undefined;
        // console.log(str, typeof str); //undefined undefined
        // str = Number(str);
        // console.log(str, typeof str); //NaN number


        // str = true;
        // console.log(str, typeof str); //true boolean
        // str = Number(str);
        // console.log(str, typeof str); //1 number

        // str = false;
        // console.log(str, typeof str); //false boolean
        // str = Number(str);
        // console.log(str, typeof str); //0 number


        // let age = Number(prompt("请输入年龄:"));
        // console.log(age, typeof age); //20 number

3、布尔转换

                 -将其他类型转换为boolean类型,调用Boolean()函数

                 - 直观上为"空"值(比如: 0 空字符串 null undefined NaN)转换为false

                 - 其他都为true

let temp = 1;
        console.log(temp, typeof temp); //1 number
        temp = Boolean(temp);
        console.log(temp, typeof temp); //true boolean

        temp = 0;
        console.log(temp, typeof temp); //0 number
        temp = Boolean(temp);
        console.log(temp, typeof temp); //false boolean

        temp = null;
        console.log(temp, typeof temp); //null Object
        temp = Boolean(temp);
        console.log(temp, typeof temp); //false boolean

        temp = undefined;
        console.log(temp, typeof temp); //undefined undefined
        temp = Boolean(temp);
        console.log(temp, typeof temp); //false boolean

        temp = "abc";
        console.log(temp, typeof temp); //abc String
        temp = Boolean(temp);
        console.log(temp, typeof temp); //true boolean

        temp = "";
        console.log(temp, typeof temp); //"" string
        temp = Boolean(temp);
        console.log(temp, typeof temp); //false boolean

        temp = " ";
        console.log(temp, typeof temp); //"" string
        temp = Boolean(temp);
        console.log(temp, typeof temp); //true boolean

        temp = "0";
        console.log(temp, typeof temp); //"0" string
        temp = Boolean(temp);
        console.log(temp, typeof temp); //true boolean

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值