DEV Community

Cover image for Arrays in JS
Sakshi
Sakshi

Posted on

4 1

Arrays in JS

Array

An array organizes items sequentially, one after another in memory.

Syntax

const array_name = [item1, item2, ...];   
Enter fullscreen mode Exit fullscreen mode

Each position in the array has an index, starting at 0.

Strengths:

Fast lookups.
Retrieving the element at a given index takes O(1)O(1) time, regardless of the length of the array.

Fast appends.
Adding a new element at the end of the array takes O(1)O(1) time, if the array has space.

Weaknesses:

Fixed size.
You need to specify how many elements you're going to store in your array ahead of time. (Unless you're using a fancy dynamic array.)

Costly inserts and deletes.
You have to "scoot over" the other elements to fill in or close gaps, which takes worst-case O(n)O(n) time.

Some languages (including Javascript) don't have these bare-bones arrays.

const cars = ["Saab", "Volvo", "BMW"];

//or

const cars = [];
cars[0]= "Saab";
cars[1]= "Volvo";
cars[2]= "BMW";

//or

const cars = new Array("Saab", "Volvo", "BMW");

Enter fullscreen mode Exit fullscreen mode

STAY TUNED FOR NEXT BLOG, HOPEFULLY IT WILL LONGER THAN THIS xD

Thanks for reading <3

Image of Wix Studio

2025: Your year to build apps that sell

Dive into hands-on resources and actionable strategies designed to help you build and sell apps on the Wix App Market.

Get started

Top comments (0)

πŸ‘‹ 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