Arrays
An array
is an object that can store multiple values in a single variable.
You can reference each value in the array by using an index
.
Creating an Array
Creating an array is simple. Just include your desired values, separated by commas ,
, within square brackets []
.
Creating an Array
const fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits); // ["Apple", "Banana", "Cherry"]
Accessing Array Values
To retrieve an item from an array, you need to use its index (a number indicating the position of an element in the array). Array indices start at 0.
Accessing Array Values
const fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits[0]); // "Apple"
console.log(fruits[1]); // "Banana"
Determining the Length of an Array
You can find out how many items are in an array by using the length
property.
const fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits.length); // 3
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.