top of page

Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

Creating Arrays

To declare an array, define the variable type with square brackets:

​

We have now declared a variable that holds an array of strings. To insert values to it, we can use an array literal - place the values in a comma-separated list, inside curly braces:

​

To create an array of integers, you could write:

Capture.PNG
Capture.PNG
wixAr4.PNG
Capture.PNG
Capture.PNG

Access the Elements of an Array

You access an array element by referring to the index number.

This statement accesses the value of the first element in cars:

wixAr5.PNG
Capture.PNG
Capture.PNG

Change an Array Element

To change the value of a specific element, refer to the index number:

Capture.PNG
Capture.PNG

Array Length

To find out how many elements an array has, use the length property:

Loop Through an Array

You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.

The following example outputs all elements in the cars array:

Capture.PNG

Dynamic Arrays

The dynamic array is a variable size list data structure. It grows automatically when we try to insert an element if there is no more space left for the new element. It allows us to add and remove elements. The array can change  and  grow or get samller on the run time.

In the example code on the left you see that Dynamic arrays are much more user friendly than Static arrays. 

bottom of page