Arrays in JavaScript
Now we will begin to study a special data type called array. An array is a variable in which you can store a whole set of some values in an ordered way.
The values that an array stores are called elements. Elements are separated by a comma. It is common practice to put spaces after this comma.
Square brackets are used to create an array:
let arr = [];
Let's fill our array with strings:
let arr = ['a', 'b', 'c'];
And now let's fill the array with numbers:
let arr = [1, 2, 3];
In addition to strings and numbers, you can store all valid data types in an array, as well as mix them together in one array, example:
let arr = [1, 2, 'a', 'b', null, true, false];
Create an array of numbers. Print
it to the screen with alert
and also log it to the console
with console.log
.
Create an array of strings. Print
it to the screen with alert
and also log it to the console
with console.log
.