# 01 JS中的数据类型分类和检测 **Published by:** [xukai](https://paragraph.com/@xulai/) **Published on:** 2021-11-10 **URL:** https://paragraph.com/@xulai/01-js ## Content 一、JS中的数据类型 1、原始值类型 string: 基于单引号、双引号、反引号「模板字符串」 包起来的都是字符串 number: NaN「不是一个有效数字」,Inifity「无穷大的值」 1. (NaN === NaN) ==>> false (NaN 与 NaN 不相等) 2. 不相等:所以不能基于是否等于NaN来判断是否为有效数字 3. isNaN([value]): 不论[value]是什么类型,都会默认隐式转换为数字类型「Number([value])」,再校验是否为有效数字,是有效数字返回false,不是有效数字返回true 4. Object.is(NaN, NaN) ==>> true(不兼容IE,Object.is内部不会做隐式类型转换) boolean: true/false undifined null symbol: 唯一值 // 特性:唯一值 let n = Symbol('A') let m = Symbol('B') console.log(n === m) // false // 三种常见的应用 // 1. 对象的唯一属性 let key = Symbol() let obj = { [key]: 100 } console.log(obj[key]) // 100 let arr = obj.getOwnPropertySymbols(obj) arr.forEach(key => { console.log(obj[key]) }) // 2. 宏观管理标识:保证标志唯一性(vuex/redux) // 3.底层原理 Symbol.hasInstance Symbol.iterator Symbol.toPrimitive Symbol.toStringTag ...... 2、对象类型 标准普通对象:object 标准特殊对象:Array/RegExp/Date/Error/Math/ArrayBuffer/DataView/Map/Set... 非标准特殊对象:String/Number/Boolean/Symbol/BigInt 可调用对象「实现了call方法」:function 二、数据类型检测 typeof: 运算符 1. 返回[value]所属类型的字符串,例如 'string'/'number' 2. 不能检测null,typeof null ==>> 'object' 3. 对象类型中除了函数会返回'function',其余都返回'object' 4. 检测一个未声明的变量不会报错,返回`undifined` instanceof: 检测某个实例是否属于某个类 constructor: 获取构造函数 Object.prototype.toString.call([value]): 检测数据类型 Array.isArray([value]): 检测是否为数据 ## Publication Information - [xukai](https://paragraph.com/@xulai/): Publication homepage - [All Posts](https://paragraph.com/@xulai/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@xulai): Subscribe to updates - [Twitter](https://twitter.com/onev_xu): Follow on Twitter