JavaScript/Arrays

In JavaScript, an array is an object where you can store a set of values under a single variable name. So far, it's the same as in many other languages. But there are distinctions. (Hint: When using arrays, you should always use the bracket notation with non-negative integers, e.g., . Technically, it's also possible to use the dot notation, but this leads - at least for beginners - to unexpected behavior.)
 * It's not necessary that the values are of the same data type. You can put everything you want in an array and worry about the data type later. (But there are also typed arrays where all values have the same data type.)
 * When you create an array, you do not need to declare a size - but you can. Arrays grow automatically. This makes arrays very convenient to use, but not well-suited for applications in numerical analysis.
 * Because arrays are objects, they have methods and properties you can invoke at will. For example, the  property indicates how many elements are currently in the array. If you add more elements to the array, the value of the   gets larger.
 * The element-counting starts at 0, which means, for instance, that the 5th element is located with [4].

Create an array
First, as with all objects, there is a constructor.

Next, the JavaScript syntax supports square brackets when creating or working with arrays.

You can predefine the size of an array when declaring it.

Access an array element
Array elements are accessed for reading or writing with the usual bracket notation.

When you access an element above the array's actual length, the size of the array will grow, and the new element will be created.

Varying data types
You can store values of different data types within an array.

Nested arrays
As shown before, an array element may be an array (which itself may contain elements of type array (which itself may contain ...)). This can occur during runtime or during initialization. To access the lower levels directly, you must specify as many bracket pairs  as necessary to reach this level.

... and a little more complex

is a property of each array (it's not a method). It represents the number of elements in that array. Please notice that array indices are zero-based. Therefore the array's length is huger than the last index.

The  method returns the combination of two or more arrays. To use it, first, you need two or more arrays to combine.

Then, make a third array and set its value to.

Note that in this example, the new arr3 array contains the contents of both the arr1 array and the arr2 array.

and
The  method returns a single string that contains all of the elements of an array &mdash; separated by a specified delimiter. If the delimiter is not specified, it is set to a comma.

There is also a  method that performs the opposite of  : it operates on a string, divides him into elements based on the specified delimiter, and returns an array that contains those elements. (Hint:  is a method of the data type string, not of array.)

To use, first make an array.

Then, make a new variable and assign it to.

You can also use a dedicated delimiter.

Convert it back into an array with the string's  method.

The  method adds one or more elements to the end of an array and returns the array's new length.

The  method removes the last element of an array and returns the element.

and  work at the end of an array; they reverse their respective effects.

The  method adds one or more new elements to the beginning of an array and returns the array's new length. It works by 'unshifting' every old element from its old index $$i$$ to $$i+1$$, adds the new element to index $$0$$, and adopts the array's  property. It is comparable with  but works at the beginning of the array.

The  method removes the first element of an array and returns the removed element. It works by 'shifting' every old element from its old index $$i$$ to $$i-1$$, adopts the array's  property, and returns the old first element. It is comparable with  but works at the beginning of the array.

and  work at the beginning of an array; they reverse their respective effects.

Exercises

 * ... are available on another page (click here).