JavaScript如何判断数组是数组?

82 阅读1分钟

1.typeof 错误方法

    let a = [1,2,3,4]
    let type = typeof(a);
    console.log(type);

打印结果

image.png

原因:对于Object类型和Array类型,typeof检测结果都是Object

2.tostring

    let a = [1,2,3,4]
    let type = Object.prototype.toString.call(a);
    console.log(type);

打印结果

image.png

3. instanceof

    let a = [1,2,3,4]
    let type = a instanceof Array;
    console.log(type);

打印结果

image.png