L
3 months ago
Lucacountry asked

How do arrays work?

Kelish Rai
Expert
2 weeks ago
Kelish Rai answered

Arrays work by storing multiple values in a single variable. Instead of creating a new variable for every value, you can group related values together in an array. This is especially useful when you have a list of items.

Here's a simple example:

const fruits = ["apple", "banana", "orange"];

In this example, fruits is an array that holds three values: apple, banana, and orange.

Here are some things that you can do with arrays:

  • Access values using their index: fruits[0]

  • Update values: fruits[1] = "mango"

  • Add values: fruits.push("grape")

  • Loop through values

Arrays are a basic but powerful tool in programming, and you’ll use them often when working with lists, collections, or structured data.

JS
This question was asked as part of the Learn JavaScript Basics course.