Because whether it is an array or an object, the return value of the typeof operation is object, so there is a need to distinguish between array types and object types:
Fang 1: Through the length attribute: Generally, the object does not have a length attribute value, its value is undefiend, and the length value of the array is of type number
Disadvantages: Very impractical, when the object The attribute of length exists and its value is number (such as array-like), then this method is invalid and is not recommended to be used. Just take a look.
*Part 2: Judge the distinction through instanceof
1.instanceof
2.constructor
3.Object.prototype.toString()
4. Array only Methods (such as: sort, pop, etc.)
The above is the detailed content of Introduction to the method of determining whether a variable is an object or an array in JavaScript. For more information, please follow other related articles on the PHP Chinese website!