Javascript array.isarray() method

Javascript array.isarray() method

isArray() is another static method in the Array class just like the .from() method. As the name suggests the purpose of the isArray(arg) method is to figure out if the argument passed to the method is an array or not.

If the argument passed to the method is of Array type the method returns true and if the argument passed to the method is not an array the method returns a false.

Array.isArray([1, 2, 3]);// true
Array.isArray({foo: 123}); // false
Array.isArray('foobar'); // false
Array.isArray(undefined);// false 

You can try experimenting the method with different values and check out the result returned.

Now since we know that the purpose of the method is find whether the value is an Array or not can we not use typeof operator available in JavaScript to find whether the value is an Array or not ?

The answer is ‘No’. The typeof operator returns the Data Type out of the nine data types available in JavaScript (undefined , Boolean , Number , String , BigInt , Symbol which are the six primitive datatypes in JavaScript and additionally null, Object and Function which completes the list of the Nine types in JavaScript)

Since Array is a Structural Data Type which is derived from the Object applying typeof on the array instance would return “Object” and not “Array”.

typeof would not return the type of the datatypes that are derived from the Object. So basically typeof would return the value as “Object” for all the datatypes that are contructed from the Object using the “new” keyword like (new Object, new Array, new Map, new Set, new WeakMap, new WeakSet , new Date).

Surprisingly Date is not a separate datatype in JavaScript like other programming languages rather it is also derived from the Object in JavaScript.