typeof的作用
- typeof是一元运算符,返回值为字符串,该字符串用来说明运算数的数据类型
- 用来获取运算数的数据类型。返回的值有number、boolean、undefined、function、object、string
- number:数字会返回number类型
- boolean:boolean值只有true和false
- undefined:当变量未被声明时会返回undefined,这与
var name;alert(name);
是不一样的。后者是指变量已声明,但未被初始化。- function:当运算数为函数时,返回function
- obeject:对象、数组、null会返回object。正因为typeof遇到数组、null都会返回object,所以我们要判断某个对象是否是数组或者某个变量是否是对象的实例时就要使用instanceof
- string:当运算数为字符串时会返回string
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png![]()
instanceof的作用
- instanceof用于判断某个变量是否是某个对象的实例,返回值为true或false
- 用法
![]()
typeof和instanceof的作用和区别
最新推荐文章于 2022-05-24 20:02:46 发布