DEV Community

Cover image for Array Methods in JS
Parwinder 👨🏻‍💻
Parwinder 👨🏻‍💻

Posted on • Edited on

Array Methods in JS

Array Utility Methods

Methods that exist on the parent Array

Array.of

const x = Array.of("Parwinder", "Bhagat"); console.log(x); // [ 'Parwinder', 'Bhagat' ]  // Example with the spread operator const y = Array.of(..."Parwinder"); console.log(y); // [ 'P', 'a', 'r', 'w', 'i', 'n', 'd', 'e', 'r' ] 

Array.from

Creates an array with the given length, items, array-like or iterable object.

const x = Array.from({ length: 3 }); console.log(x); // [ undefined, undefined, undefined ] // With items using a callback function as second parameter const y = Array.from({ length: 3 }, function () { return "Hello"; }); console.log(y); // [ 'Hello', 'Hello', 'Hello' ] // With items using a callback function and arguments const y = Array.from({ length: 3 }, function (value, index) { return index; }); console.log(y); // [ 0, 1, 2 ] 

Array.isArray()

typeof an array returns object as a type, so this is where isArray comes in handy.

const x = ["Hello", "World"]; console.log(Array.isArray(x)); // true const y = 27; console.log(Array.isArray(y)); // false 

Top comments (0)