Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Arrays | Section
JavaScript Essentials for React Native Development

bookArrays

Stryg for at vise menuen

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"]
copy

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.

question mark

Which statement about arrays in JavaScript is true?

Vælg det korrekte svar

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 6

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Sektion 1. Kapitel 6
some-alt