image.png
Js有7种数据类型string、number、boolean、null、undefined、symbol(ES6新增)、object、bigInt
基本数据类型有: string、number、boolean、null、undefined
引用类型Object: Array、Date、function
类型 | typeof | 例子 | 备注 |
---|---|---|---|
sting | sting | typeof 'zs' => 'string' | |
number | number | typeof 2 => 'number' | |
boolean | boolean | typeof true =>"boolean" | |
null | object | typeof null => 'object' | 不存在的对象 |
undefined | undefined | typeof undefined => 'undefined' | 未定义 |
NaN | number | typeof NaN => 'NaN' | Number 中的特殊数值 |
skk // 在浏览器下直接会报错,因为没有定义,引用未定义的会直接提示错误
var skk
undefined
typeof skk
"undefined"
skk = null
null
typeof skk
"object"
skk = 'zs'
"zs"
typeof skk
"string"
1. js中的isNaN 与Number.isNaN的区别
isNaN: 当我们向isNaN传递一个参数,它的本意是通过Number()方法尝试将这参数转换成Number类型.
如果成功返回false,如果失败返回true。
所以isNaN只是判断传入的参数是否能转换成数字,并不是严格的判断是否等于NaN
isNaN('zs')
true // Number(‘zs’) 不能转换成 Number类型,因此返回true
isNaN('12')
false // Number('12')可以转换成数字 则返回了false
Number.isNaN: 判断传入的参数是否严格的等于NaN(也就是 ===)
Number.isNaN(NaN)
true // 判断传入的参数严格等于 NaN才返回true,其余返回false
2. null 和 undefined的区别:
image.png
null:表示值定义了,但是是个空值,没有对象
(1)作为原型链的终点
(2)作为函数的参数,表示该函数的参数不是对象(todo)
Number(null)
0
Object.getPrototypeOf(Object.prototype)
null // 原型链终点
undefined: 此处应该有一个值,但是还没有定义
(1)变量被声明了,但没有赋值时,就等于undefined
(2) 调用函数时,应该提供的参数没有提供,该参数等于undefined
(3)对象没有赋值的属性,该属性的值为undefined
(4)函数没有返回值时,默认返回undefined
var zs
undefined
zs
undefined // 定义了但未赋值
-------------------
function zs (x) {
return x
}
zs()
undefined // 调用函数未提供参数
-------------------
var obj = {}
obj.zs
undefined // 对象obj中无‘zs’这个属性
-------------------
function zs (a) {
a = a + 1
}
zs(1)
undefined // 函数没有返回值(无return)
3. typeof 与 instanceof的区别
image.png
let arr = [1,2]
let obj = {zs: 1}
undefined
typeof arr
"object"
typeof obj
"object"
arr instanceof Array
true
obj instanceof Object
true
obj.__proto__ === Object.prototype
true // 原型链概念