DEV Community

loading...

Array, A Drop Of Golden Sun...

mariel
Dev. I love dogs, cookies, and learning new things.
・1 min read

Originally published on marieldotcode.com on 9/16/19

An array is a powerful way to organize information with multiple ways it can be manipulated. Arrays will be encountered frequently in web development, as they store a collection of objects. The details of how arrays are created, accessed, and manipulated will vary between programming languages, but the most basic concepts are discussed below.

Square brackets ([ ]) are used to create and symbolize arrays. Each object in an array is an element, and elements are separated by commas.

[Element1, Element2, Element3]
Enter fullscreen mode Exit fullscreen mode

Arrays begin “counting” their elements using a “0 index", meaning that the first element is actually 0, with the second element being 1, and so on. The example above, replacing the numbers of the elements with the appropriate index numbers, would be:

[Element0, Element1, Element2]
Enter fullscreen mode Exit fullscreen mode

Elements may be referred to by their index numbers, and arrays may be assigned to variables so they may be referenced more easily. Using the example above:

example = [Element0, Element1, Element2]
Enter fullscreen mode Exit fullscreen mode

To reference the first element, we may use:

example[0]
Enter fullscreen mode Exit fullscreen mode

Arrays have several properties, including length. Accessing an array’s length can tell you how many elements it contains.

A frequent manipulation of arrays is adding an element or removing an element. Elements are pushed on to the end, and popped off the end. Depending on the language you are using, the first element in an array may be removed via shift and added via unshift.

Discussion (0)