본문으로 건너뛰기

Arrays

Arrays

An Array is an object that can store multiple values within a single variable. Each value in an array can be referenced by an index.


Creating an Array

Creating an array is simple. You just put the desired values inside square brackets [], separated by commas ,.

let 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, which starts at 0.

let fruits = ['apple', 'banana', 'cherry'];
console.log(fruits[0]); // "apple"
console.log(fruits[1]); // "banana"

Finding Out the Length of an Array

You can find out how many items are in an array by using the length property.

let fruits = ['apple', 'banana', 'cherry'];
console.log(fruits.length); // 3