Distinguishing arrays from objects in JavaScript
As mentioned above, the typeof operator
does not distinguish between arrays and objects.
Sometimes, however, this needs to be done.
The function Array.isArray() will
come to the rescue:
console.log( Array.isArray([]) ); // shows true
console.log( Array.isArray({}) ); // shows false
Without running the code, determine what will be displayed on the console:
console.log( Array.isArray([1, 2, 3]) );
Without running the code, determine what will be displayed on the console:
console.log( Array.isArray({x: 1, y: 2, z: 3}) );