JavaScript数据类型

一、分类:

JavaScript数据类型分为两大类:基本数据类型和引用数据类型。

基本数据类型(值类型):

字符串(String):任意字符串

数字(Number):任意数字

布尔值(Boolean):true/false

空(Null):null

未定义(Undefined):undefined

Symbol(ES6 表示独一无二的值)

BigInt(ES10表示任意大的整数)

引用数据类型(对象类型):

object(Object、Array、Function、RegExp、Data)

二、判断:

①typeof:

可以判断String、Number、Boolean、Undefined、Function

    var str = 'abcd'
    console.log(typeof str === 'string')  //true
    var num = 1
    console.log(typeof num === 'number')  //true
    var a = true
    console.log(typeof a === 'boolean')  //true
    var b
    console.log(typeof b === 'undefined')  //true
    var obj = {
      obj1: [1, 'abc', console.log],
      obj2: function () {
        console.log('obj2')
      }
    }
    console.log(typeof obj.obj2 === 'function')  //true

不能判断Null与Object、Array与Object

    var obj = {
      obj1: [1, 'abc', console.log],
      obj2: function () {
        console.log('obj2')
      },
    }
    var n = null
    console.log(typeof n === 'object')  //true
    console.log(typeof obj.obj1 === 'object')  //true

②instanceof

判断对象具体类型

    var obj = {
      obj1: [1, 'abc', console.log],
      obj2: function () {
        console.log('obj2')
      }
    }
    console.log(obj.obj1 instanceof Array)  //true
    console.log(obj.obj2 instanceof Function)  //true
    console.log(obj instanceof Object)  //true

③===

判断Undefined、Null

三、null和undefined的区别:

undefined:代表定义未赋值

null:代表定义并赋值了,只是值为null

    var a
    console.log(a)  //undefined
    var b = null
    console.log(b)  //null

什么时候给变量赋值为null?

 console.log(typeof null)  //object

null是一个基本数值类型,但是通过typeof判断null是一个对象Object,这就产生了矛盾。

当我准备一个变量,后面要给他赋值为对象,但是现在对象还没有产生,先给它赋一个null。表明将要给这个变量赋值为对象。

null还可以释放内存。

    // 开始赋值为null,表明将要赋值为对象
    var b=null
    // 确定对象赋值
    b=['abc',11]
    // 最后,释放内存
    b=null

所以,null在初始赋值时,表明将要赋值为对象;结束前,让对象成为垃圾对象,被垃圾回收器回收。

1.对于基本数据类型中的Symbol,它是ES6 新增的,表示独一无二的值。它有以下几个特点:

①语法:Symbol('des')  

括号里面为描述信息,可以是数字或字符串,可以写也可以不写。

②Symbol不是构造函数,不能使用new,会报错

let s = new Symbol('des')

③ Symbol( )   括号中代表对Symbol的描述,并不是Symbol的值

let s = Symbol(22)
console.log(s === 22)

 ④每个Symbol的值是唯一的,不会相等

let s = Symbol(22)
let s1 = Symbol(22)
console.log(s === s1)

 ⑤不能进行隐式转换,不能拼接字符串,可显式转换为字符串,不能转布尔值,也不能转数字

    let s = Symbol(22)
    console.log(s + 1) //报错
    console.log(s + '1')  //报错
    console.log(s.toString())  //Symbol(22)
    console.log(Boolean(s))  //报错
    console.log(Number(s))  //报错

2.对于基本数据类型中的BigInt,它是ES10 新增的,表示任意大的整数。

在JS中,Number无法精确表示非常大的整数,它会将非常大的整数四舍五入,确切地说,JS中的 Number类型只能安全地表示-9007199254740991(-(2^53-1))和9007199254740991((2^53-1)),任何超出此范围的整数值都可能失去精度。

console.log(9999999999999999999)

 同时,也会出现安全问题

 console.log(9678987655277356 === 9678987655277357)
    
 //true

创建语法BigInt的语法为:BigInt() 

let num = BigInt('9678987655277356')
console.log(num)

//9678987655277356n

还有另一种方式,在末尾直接加n

 console.log(9678987655277356n)
    
 //9678987655277356n

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值