What is a JavaScript array?

A JavaScript array is, simply put, a list of stuff.

For example a list of strings containing some names of type of fruits:

['apple', 'lemon', 'pear']

As we see, an array can be created by using square brackets.

The items in an array is ordered. Which means that

[0, 1, 2]

is not the same as

[0, 2, 1]

The same item may exist several times in an array.

[1, 5, 1]

Let's try it out

1

Type the following into the the box where it says Enter source code here:

turnOnMultiple([0, 2]);
turnOffMultiple([0, 2]);
2

Click Check source code.

3

Click Run!.

So what is happening here?

The functions turnOnMultiple() and turnOnMultiple() are taking one argument each - an array of numbers identifying the lamps to turn on and off respectively when the functions are called.

Note that in programming, one almost allways start counting on zero, not one.

Now, for fun. Try this:

1
2
3
4
5
6
turnOnMultiple([0, 1]);
turnOffMultiple([0, 1]);
turnOnMultiple([1, 2]);
turnOffMultiple([1, 2]);
turnOnMultiple([2, 3]);
turnOffMultiple([2, 3]);

Oh, well. Try this as well:

turnOnMultiple([0, 1, 2, 3]);
turnOffMultiple([0, 1, 2, 3]);
Next lesson:
JavaScript objects
What is a JavaScript object?