Forem

Cover image for Array Methods in JS
Parwinder πŸ‘¨πŸ»β€πŸ’»
Parwinder πŸ‘¨πŸ»β€πŸ’»

Posted on β€’ Edited on

3 1

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

Image of Timescale

πŸš€ pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applicationsβ€”without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Image of Docusign

πŸ› οΈ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

πŸ‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay