Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Arrayer | Seksjon
Javascript-Grunnleggende

bookArrayer

Sveip for å vise menyen

Arrays are a fundamental data structure in JavaScript that allow you to store and organize multiple values in a single variable. You use arrays when you need to keep track of a list of items, such as numbers, strings, or even other arrays.

Arrays make it easy to work with collections of data, whether you are building a shopping cart, tracking scores in a game, or managing user input. Each value in an array is called an element, and you access these elements using their position, known as the index.

123456789
// Create an array of colors const colors = ["red", "green", "blue"]; console.log(colors); // Access the first element (index 0) console.log(colors[0]); // Output: "red" // Access the third element (index 2) console.log(colors[2]); // Output: "blue"
copy
Note
Note

In JavaScript, array indexes start at 0, so the first element is at index 0, the second at index 1, and so on.

You can find out how many elements are in an array by using the length property. For example, if an array has three items, its length will be 3. You can also use the length property to access the last element of the array by using array[array.length - 1]. This makes it easy to work with arrays of any size, since you do not need to know in advance how many elements they contain.

12345678
// Create an array of fruits const fruits = ["apple", "banana", "cherry", "date"]; // Find the number of elements in the array console.log(fruits.length); // Output: 4 // Access the last element using array[array.length - 1] console.log(fruits[fruits.length - 1]); // Output: "date"
copy
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 11

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 1. Kapittel 11
some-alt