2021-03-02

** 判断数据类型**
typeof 关键字来进行判断(两个使用方式)
1.typeof 你要检测的变量
2.typeof (你要检测的变量)
typeof关键字的返回值就是你检测的这个变量的数据类型
typeof关键字的返回值是一个什么数据类型(返回值是一个字符串)
当出现两个以上的typeof关键字的时候,都是string
typeof只能检测基本数据类型,到复杂数据类型和null的时候不能准确检测
var n1=100
表示我要检测n1的数据类型
console.log(typeof n1)
console.log(typeof(n1))
res 这个变量接受的内容就是检测n1变量的结果
var res =typeof n1
console.log(typeof res)
console.log(typeof typeof typeof n1)
数值类型
var n1 = 100
var n2 = NaN
console.log(typeof n1)
console.log(typeof n2)
字符串数据类型
var s1 =‘dsadsafads’
var s2 =’ %%^^ ^$%aad’
var s3 =‘100’
console.log(typeof s1)
console.log(typeof s2)
console.log(typeof s3)
布尔数据类型
var b1 = true
var b2 = false
console.log(typeof b1)
console.log(typeof b2)
复杂数据类型
var u
var n=null
console.log(typeof u)
console.log(typeof n)

2.转换成数值数据类型
方法1.Number(你要转换的内容)
返回值的形式接受结果,返回值就是转换后的内容
在转换的过程中,会把你要转换的内容当做一个整体来看待
能转换成功就是一个数值,不能成功就是NuN
var s1=‘100’
var res1=Number(s1)
console.log(res1)
console.log(typeof res1)

    console.log('======================')

    var s2='100a'
    var res2=Number(s2)
    console.log(res2)

    console.log('======================')

    var  u
    var res3=Number(u)
    console.log(res3)

    console.log('======================')

    var b1=true
    var b2=false
    var res4=Number(b1)
    var res5=Number(b2)
    console.log(res4)
    console.log(res5)
    
     方法2:parseInt(你要转换的数据)
     结果以返回值的形式得到
     转换的过程中,会把整个东西当做一个完整的文本来看待
     从左到右一位一位的看
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值