🌈 基本类型
- undefined | null
- String | Number | Boolean
- Symbol 「ECMAScript 2015(ES6第六版)」
- Bigint「es10」
复杂类型
- Object
- Array,Date,RegExp,Function
- 基本包装类型
- 单体内置对象
🌈 区别
两种类型的区别是:存储位置不同;
- 值类型存储在栈(stack)中,占空间小、大小固定,属于被频繁使用数据,所以放入栈中存储;
- 引用类型存储在堆(heap)中,占据空间大、大小不固定。如果存在栈中,影响程序运行性能;引用类型在栈中存储了指针,该指针指向堆中该实体的起始地址。当解释器寻找引用值时,会首先检索其在栈中的地址,取得地址后从堆中获得实体。
- 栈(stack):是栈内存的简称,栈是自动分配相对固定大小的内存空间,并由系统自动释放,栈数据结构遵循FILO(first in last out)先进后出的原则
- 堆(heap):是堆内存的简称,堆是动态分配内存,内存大小不固定,也不会自动释放,堆数据结构是一种无序的树状结构
JS 类型检测
🌈 typeof 检测某个值是什么类型
- 8 种返回值
- undefined
- string | number | boolean
- symbol | bigint
- function
- object(包含 array、null)
- typeof 返回的值是字符串类型
- typeof 一个不存在(未定义)的值,不报错,返回字符串
"undefined"
console.log(typeof [] === 'object'); // true
console.log(typeof null === 'object'); // true
console.log(typeof(undefined)); // undefined
console.log(typeof(NaN)); // number