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.
2
Answers
OP Here, The answer is derived from the referred question.
isArrayLike
will returntrue
for only array-like objects, andfalse
for arrays or other type of objects.arguments in a function is an object with keys and values you do not need to check if it is array like.
If you want to sum every value passed to function sum just loop throuht arguments using for..in loop
Make sure every arguments[key] is a number which can be summed otherwise strings will be concatenated