第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

數(shù)據(jù)類型轉換[上][JavaScript之美]

標簽:
JavaScript
什么是类型转换?
自动(隐式类型转换)或强制(显示类型转换)改变变量类型的操作/运算【个人理解,仅供参考】

继续之前,你应该知道基本数据类型(也叫基本数据类型的包装类型):Number,String,Boolean
还有两个特殊的Null,Undefined。

类型转换分类:
  1. 隐式类型转换
  2. 显示类型转换
隐式类型转换: Number:

String(字符串)---->Number:

//使用方式:
    //得到一个数值
        //1. 字符串(其中的字符必须为Number类型)通过 -、*、/、%与数值型(Number类型)进行算术运算
        //2. 字符串(其中的字符必须为Number类型)通过 -、*、/、%与字符串型(字符为Number类型)进行算术运算
    //得到NaN(Not a Number: 不是一个数字的数值类型的值)
        //3. 字符串(其中的字符不全为Number类型)通过 -、*、/、%与数值型(Number类型)进行算术运算
var num = "123" / 1; //结果为:"num=123, typeof=number" , 方式1的测试
num = "123" / "123"; //"num=123, typeof=number" , 方式2的测试
num = "123a" / 1; //"num=NaN, typeof=number" , 方式3的测试
console.log("num=" + num + ", typeof=" + typeof num);

Boolean(布尔值)---->Number:

//使用方式:
    //得到一个数值
        //1. true----> 1
        //2. false----> 0
var num = false * 1; //结果为:"num=0, typeof=number", 方式1的测试
num = true + 1; //结果为:"num=2, typeof=number",  方式2的测试
console.log("num=" + num + ", typeof=" + typeof num);

Null(空值)---->Number:

//使用方式:
    //得到一个数值
        //1. null---->0
var num = null * 1;//结果为:"num=0, typeof=number"
console.log("num=" + num + ", typeof=" + typeof num);

Undefined(未定义)---->Number:

//使用方式:
    //得到一个NaN
    //1. undefined---->NaN
var num = undefined * 1;//结果为: "num=NaN, typeof=number"
console.log("num=" + num + ", typeof=" + typeof num);
String:

基本数据类型---->String:

//使用方式:
    //通过字符串连接符进行运算时
var str = 1 + "abc";//结果为:"num=1abc, typeof=string"
console.log("str=" + str + ", typeof=" + typeof str);

引用类型数据---->String:

//使用方式:
    //对引用类型数据进行输出时(console.log(),document.write(), alert()),默认会调用toString()方法,返回一个字符串
var arr = [1, 2, 3];
//alert(arr);
//document.write("arr=" + arr);
console.log(arr);
Boolean:
//为了方便,这里定义一个函数:(解释一下,就是看看你传入的值是不是会自动进行转换为布尔值(true/false), 
    //因为if条件语句中表达式结果只能为true/false)
    function fnIsBool(iValue) {//iValue会不会自动转换为布尔型的值?
        if(iValue) {//true
            console.log("bool=" + true + ", typeof=" + typeof iValue);
        } else {//false
            console.log("bool=" + false + ", typeof=" + typeof iValue);
            }
    }

Number---->Boolean:

fnIsBool(0);//输出为:false
fnIsBool(0.0);//输出为:false
fnIsBool(NaN);//输出为:false

Stirng---->Boolean:

fnIsBool("");//空字符串 //输出为:false

Null---->Boolean:

fnIsBool(null);//输出为:false

Undefined---->Boolean:

fnIsBool(undefined);//输出为:false

总结一句话:除了以上6中情况为false外,其他的都会隐式转换为true

點擊查看更多內容
9人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質文章

正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消