JavaScript的类型可以分为原始类型和对象类型,也可以分为可以拥有方法的类型和不能拥有方法的类型,同样可以分为可变类型和不可变类型。
分类
| 原始值类型 | 对象值(引用)类型 | |
|---|---|---|
| 数据类型 | 字符串(String)、数字(Number)、布尔(Boolean)、对空(Null)、未定义(Undefined)、Symbol | 对象(Object)、数组(Array)、函数(Function)、日期(Date)、正则(RegExp) |
| 位置 | 存储在栈中的简单数据段,他们的值直接存储在变量访问的位置。 在内存中占据固定大小,保存在栈内存中。 | 存储在堆中的对象,存储在变量处的值是一个指针,指向存储对象的内存处。 |
| 其他 | 原始值是不可更改的:任何方法都无法更改一个原始值 | 可修改的 |
判断方法
typeof:可以用来区分除了null类型以外的原始类型数据
1. 原始值类型
typeof ''; //'string'
typeof 1; //'number'
typeof true; //'boolean'
typeof null; //'object'
typeof undefined; //'undefined'
typeof Symbol; //'function'
typeof Symbol(); //'symbol'
2. 引用数据类型
typeof function(){} //'function'
typeof Date //'function'
typeof RegExp //'function'
typeof [] //'object'
typeof {} //'object'