Arrays
Pyyhkäise näyttääksesi valikon
Arrays are ordered lists of values, stored in a single variable. You can think of an array as a collection of items, such as a list of fruit names, where each item has a specific position called an index. In JavaScript, array indexes start at 0, so the first element is at index 0, the second at index 1, and so on.
12345678910// Creating an array const fruits = ["apple", "banana", "cherry"]; // Accessing elements by index console.log(fruits[0]); // "apple" console.log(fruits[2]); // "cherry" // Modifying values fruits[1] = "blueberry"; console.log(fruits); // ["apple", "blueberry", "cherry"]
Accessing a value in an array is done by referencing its index inside square brackets. For example, fruits[0] gives you the first fruit in the array, while fruits[2] gives you the third. You can also change the value at a specific index by assigning a new value to it, like fruits[1] = "blueberry", which replaces the second fruit.
Arrays are commonly used to store lists of related data, such as items in a shopping cart, user names, or scores in a game. Their ability to hold multiple values and let you access or change them by index makes arrays a powerful tool for managing collections in your code.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme