Skip to main content

Arrays

@serverSerrverlesskiy

To store ordered collections, there is a special data structure called an Array.

Storage

Array - an ordered collection of data, which contains the 1st, 2nd, 3rd elements, etc. For example, we need it to store πŸ“¦ a list of something: users, products, site elements, etc.

Creation#

create

There are 2️⃣ options for creatingπŸ—οΈ an empty array:

let arr = new Array(5)
// new Array(5) - creates an array with no elements (which cannot be accessed just like that), but with a given length.
let arr = []

The second variant 2️⃣ syntaxπŸ“– is almost always used. In parentheses, we can indicate the initial values of the elements:

Live Editor
Result
SyntaxError: Unexpected token (1:8)
1 : return ()
            ^

The array elements are numbered starting from zero 0️⃣.

We can get an element by specifying its number in square brackets πŸ‘‡:

Live Editor
Result
SyntaxError: Unexpected token (1:8)
1 : return ()
            ^

We can replace the πŸ–ŠοΈ element:

fruits[2] = 'Plum' // now ["Apple", "Orange", "Plum"]

... Or add πŸ†• a new one to the existing array πŸ‘‡:

Live Editor
Result
SyntaxError: Unexpected token (1:8)
1 : return ()
            ^

Payment#

Now you are on a stripped-down version of the site, after subscribing to Patreon, you will get full access to the training course, as well as access to our server's private channels in Discord.

Download our mobile application or get tested in our JavaScript telegram bot, and also subscribe to our news.

Become a Patron!

Sumerian school

Links#

  1. Article "Arrays"
  2. MDN web doc. Article "Arrays"
  3. JavaScript Arrays
  4. Code for Teens: The Perfect Beginner's Guide to Programming, Volume 1: Javascript - Jeremy Moritz

Contributors ✨#

Thanks goes to these wonderful people (emoji key):


Dmitriy K.

πŸ“–

Dmitriy Vasilev

πŸ’΅

Resoner2005

πŸ› 🎨 πŸ–‹

Navernoss

πŸ–‹ πŸ› 🎨

Become a Patron!