Skip to content
快看这页儿写了啥...

JS 判断数组类型的方式有哪些?

题干

数组类型判断

题解

  • Array.isArray()

Array.isArray() 方法用于确定传递的值是否是一个 Array

js
Array.isArray([1,2,3]) // true
Array.isArray("hello") // false
  • instanceof

instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上

js
console.log([] instanceof Array) // true
console.log(new Date() instanceof Date) // true
  • Object.prototype.toString.call() 方法

Object.prototype.toString.call() 方法返回一个表示对象的字符串。

js
console.log(Object.prototype.toString.call([])) // [object Array]

相关

JS 数据类型判断的方式有哪些

JS 实现类型判断方法

如何判断一个对象是否属于某个类

贡献者

isboyjc's avatar isboyjc

浏览量(PV)  次  ·  独立访客(UV)  人次
不正经的前端 | 八股 · 欢迎 star ⭐