Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Arrayer | Sektion
Javascript-Grunder

bookArrayer

Svep för att visa menyn

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
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 11

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Avsnitt 1. Kapitel 11
some-alt