JS基本数据类型用法及区别

基本数据类型

  • Number

    可为任意数

//当a为正整数时
var a = 1
console.log(a, typeof a, typeof a === "number")//1 "number" true
//当a为小数时
a = 6.8
console.log(a, typeof a, typeof a === "number")//6.8 "number" true
//当a为负数时
a = -1
console.log(a, typeof a, typeof a === "number")//-1 "number" true
  • String

    可为一切字符串

    //当a为一个字符串时
    var a = "阿Q"
    console.log(a, typeof a, typeof a === "string")//阿Q string true
    
  • Boolean

    布尔类型(true/false)

    var a = true
    console.log(a, typeof a, typeof a === "boolean")//true "boolean" true
    a = false
    console.log(a, typeof a, typeof a === "boolean")//false "boolean" true
    
  • Undefined

    未定义类型—定义变量未赋值

var b
console.log(b, typeof b, typeof b === "undefined")//undefined "undefined" true
  • Null

    空类型

    var b = null
    console.log(b, typeof b, b === null)//null 'object' true
    

猜你喜欢

转载自blog.csdn.net/weixin_45753871/article/details/109365836
今日推荐