We can check whether a variable is array-like or array by Checking if an object is array-like.
How to check if a variable is array-like but not an array?
Update 1:
What I have tried so far is:
const car = { type: "Fiat", model: "500", color: "white" };
const cars = ["Saab", "Volvo", "BMW"];
function sum(a, b, c) {
console.log(Array.prototype.isPrototypeOf(arguments));
return a + b + c;
}
console.log(Array.prototype.isPrototypeOf(car));
console.log(Array.prototype.isPrototypeOf(cars));
result = sum(1, 2, 3)
I have use function sum because the documentation says:
arguments is an array-like object
So far it yields correct result. Please let me know whether I am on the right track.
&& !Array.isArray(variable)!(variable instanceof Array).Array.prototype.forEachmethod then it is array-like..forEach()it doesn't mean it's array-like.