Check if a value is an object in JavaScript

后端 未结 30 3787
臣服心动
臣服心动 2020-11-22 05:06

How do you check if a value is an object in JavaScript?

30条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 05:50

    Object.prototype.toString.call(myVar) will return:

    • "[object Object]" if myVar is an object
    • "[object Array]" if myVar is an array
    • etc.

    For more information on this and why it is a good alternative to typeof, check out this article.

提交回复
热议问题