06JavaScript
Arrays - Storing Multiple Values
Easy10MIN
What Is an Array?
An array is a special kind of variable that lets you store a list of values together. Instead of a single wire that connects a name to one value, imagine an array as a long wire with many slots—each holding a single value. These slots are called indices, and they are numbered starting at 0.
For example, consider the following array:
JAVASCRIPT
1let fruits = ["apple", "banana", "cherry"];In this code we declare a variable fruits using **let **and assign it an array of three string values. Each value is stored at a specific index:
- "apple" is at index 0
- "banana" is at index 1
- "cherry" is at index 2
Creating an Array
To create an array, you use square brackets with a comma-separated list of values:
JAVASCRIPT
1let numbers = [1, 2, 3, 4];You can also create an empty array and add values later:
JAVASCRIPT
1let emptyArray = [];
2emptyArray[0] = "first";
3emptyArray[1] = "second";1 / 6
Hakatemia Pro
Learn to hack — start here
Hundreds of interactive courses, virtual labs and CTF challenges in your browser. Start a free trial — no card required.