Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - JavaScript Arrays


JavaScript Arrays

An array stores multiple values in a single variable. You can think of it as a list of items.


Creating an Array

You create an array using square brackets [ ], and separate the items with commas.

Example

Create an array of fruits:

const fruits = ["Apple", "Banana", "Cherry"];
document.getElementById("demo").innerHTML = fruits;
Try it Yourself »

The array fruits holds three values. Each value has an index number starting from 0.


Accessing Array Elements

You can get a specific value from an array by using its index number inside brackets.

Example

Access the first item in the array:

const fruits = ["Apple", "Banana", "Cherry"];
const first = fruits[0];
document.getElementById("demo").innerHTML = first;
Try it Yourself »

Array indexes start at 0, so the first item is at position 0, the second at 1, and so on.


Changing Array Elements

You can change an element by referring to its index and assigning a new value.

Example

Change an array element:

const fruits = ["Apple", "Banana", "Cherry"];
fruits[1] = "Mango";
document.getElementById("demo").innerHTML = fruits;
Try it Yourself »

This replaces "Banana" with "Mango" in the array.

Note: The array is declared with const, but you can still change its contents. You just cannot reassign the whole fruits variable to a new array.


Array Length

The length property tells you how many items are in the array.

Example

Find the number of items in an array:

const fruits = ["Apple", "Banana", "Cherry", "Mango"];
const count = fruits.length;
document.getElementById("demo").innerHTML = count;
Try it Yourself »

Here, length is 4 because there are four items in the array.


Looping Through Arrays

You can use a for...of loop to go through each item in an array.

Example

List all fruits:

const fruits = ["Apple", "Banana", "Cherry"];
let text = "";
for (const fruit of fruits) {
  text += fruit + "<br>";
}
document.getElementById("demo").innerHTML = text;
Try it Yourself »

The for...of loop goes through all items one by one and gives you each value directly.

Tip: For arrays, for...of is a simple and readable way to loop through all values. Use a regular for loop only when you really need the index number.


Adding and Removing Elements

Arrays have built-in methods to add or remove items.

MethodDescriptionExample
push() Adds an item to the end fruits.push("Kiwi")
pop() Removes the last item fruits.pop()
unshift() Adds an item to the beginning fruits.unshift("Lemon")
shift() Removes the first item fruits.shift()

Example

Add and remove items:

const fruits = ["Apple", "Banana"];
fruits.push("Cherry"); // add to end
fruits.unshift("Lemon"); // add to start
document.getElementById("demo").innerHTML = fruits;
Try it Yourself »

After these changes, the array will contain: Lemon, Apple, Banana, Cherry.


Sorting Arrays

You can sort array items alphabetically using sort().

Example

Sort an array alphabetically:

const fruits = ["Banana", "Apple", "Cherry"];
fruits.sort();
document.getElementById("demo").innerHTML = fruits;
Try it Yourself »

The sort() method changes the array to: Apple, Banana, Cherry.


Finding Elements

You can find items in an array with methods like indexOf() and includes().

Example

Check if an item exists:

const fruits = ["Apple", "Banana", "Cherry"];
const result = fruits.includes("Banana");
document.getElementById("demo").innerHTML = result;
Try it Yourself »

This returns true because "Banana" is in the array.


Summary

  • Arrays hold multiple values in one variable.
  • Access items with [index] (starting from 0).
  • Use length to count items.
  • Add or remove with push(), pop(), shift(), and unshift().
  • Loop through arrays using for...of for simple, readable code.
  • Sort and search arrays with sort() and includes().

Next, you'll learn about JavaScript Objects — how to store data in name:value pairs.

Next » JavaScript Objects


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.