一、数据类型
基本数据类型有
Number String Boolean Null Undefined Symbol(ES6新增数据类型) bigInt
引用数据类型统称为Object类型,有
Object Array Date Function RegExp
存放位置:
基本数据类型的数据直接存储在栈中;引用数据类型的数据存储在堆中,在栈中保存数据的引用地址,这个引用地址指向的是对应的数据,以便快速查找到堆内存中的对象。
二、类型转换
1.typeof
能判断基础数据类型(null除外),引用数据类型只能判断function
缺点:typeof null的值为Object,无法分辨是null还是Object
typeof undefined // 'undefined'
typeof '10' // 'String'
typeof 10 // 'Number'
typeof false // 'Boolean'
typeof Symbol() // 'Symbol'
typeof Function // ‘function'
typeof null // ‘Object’
typeof [] // 'Object'
typeof {} // 'Object'
2.instanceof
返回一个布尔值,能准确判断引用数据类型,但是不能准确判断基本数据类型
缺点:只能判断对象是否存在于目标对象的原型链上
function Foo() { }
var f1 = new Foo();
var d = new Number(1)
console.log(f1 instanceof Foo);// true
console.log(d instanceof Number); //true
console.log(123 instanceof Number); //false -->不能判断字面量的基本数据类型