为什么 Javascript 在控制台.log之后没有被调用

Why Javascript is not getting called after console.log

本文关键字:调用 之后 log Javascript 控制台 为什么      更新时间:2023-09-26

我正在尝试创建一个控制台.log对象或数组类型的函数

function whichDataStructure (ITEM){
     if (typeof  ITEM ==='object'){
        console.log ('I am object');
   } if (typeof  ITEM === 'array') {
    console.log ('i am array');
   } else {
    console.log(' neither');

  }
};

在Javascript中,数组实际上是一种对象。

您必须使用 Array.isArray() 函数来确定值是否为 Array:

function whichDataStructure(item) {
    if (Array.isArray(item)) {
        console.log('I am an Array');
    } else if (typeof item === 'object'){
        console.log('I am an Object');
    } else {
        console.log('I am of type: ' + typeof item);
    }
};
在测试值是否

为对象之前,测试该值是否为数组非常重要。否则,它将始终被视为对象。