To pass a datatype that varies (from an array to an integer) to the same function, and then check against the datatype before changing its value, looking at the below method using instanceof Array is there a better/more efficient way?
function foo(x) {
if (x instanceof Array) {
for(i=0;i<x.length;i++){
x[i].bar = '1';
x[i].baz = '2';
}
}
else{
x.bar = '1';
x.baz = '2';
}
}
Thanks :)
Object.prototype.toString.call(x) === "[object Array]", this is an alternative method, but even yours is goodArray.isArray(x)