loading...
Cover image for Array Methods in JS

Array Methods in JS

bhagatparwinder profile image Parwinder 👨🏻‍💻 Updated on ・1 min read

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

Posted on by:

bhagatparwinder profile

Parwinder 👨🏻‍💻

@bhagatparwinder

Happy, cheerful, confident. Web developer based out of Chicago :)

Discussion

markdown guide