JavaScript Arrays

Back to JS page

Table of Contents

Creating an Array

An array is a special variable that can hold more than one value. There are two ways to create an array:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Creating an Array</h4>
<p id="demo"></p>

<script>
// Array literal (preferred)
const cars = ["Volvo", "BMW", "Toyota"];

// Using the new keyword (not recommended)
const fruits = new Array("Apple", "Banana", "Orange");

document.getElementById("demo").innerHTML =
  "Cars: " + cars + "<br>" +
  "Fruits: " + fruits + "<br><br>" +
  "Using the array literal is the recommended way";
</script>

</body>
</html>

Example 1

Result View Example

Accessing and Changing Array Elements

Array elements are accessed by their index number (starting from 0) and can be changed:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Accessing and Changing Array Elements</h4>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
const cars = ["Volvo", "BMW", "Toyota"];

// Access elements by index (0-based)
const firstCar = cars[0];
const lastCar = cars[2];

// Change an element
cars[1] = "Honda";

document.getElementById("demo1").innerHTML =
  "First car: " + firstCar + "<br>" +
  "Last car: " + lastCar;

document.getElementById("demo2").innerHTML =
  "After cars[1] = 'Honda': " + cars;
</script>

</body>
</html>

Example 2

Result View Example

Converting an Array to a String

The toString() method converts an array to a comma-separated string:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Converting an Array to a String</h4>
<p id="demo"></p>

<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];

// toString() joins with commas
const fruitString = fruits.toString();

document.getElementById("demo").innerHTML =
  "Array: " + fruits + "<br>" +
  "toString(): " + fruitString + "<br>" +
  "Type: " + typeof fruitString;
</script>

</body>
</html>

Example 3

Result View Example

The length Property

The length property returns the number of array elements. It is always one more than the highest array index:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The length Property</h4>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];

document.getElementById("demo1").innerHTML =
  "Array: " + fruits + "<br>" +
  "length: " + fruits.length + "<br><br>" +
  "First element: " + fruits[0] + "<br>" +
  "Last element: " + fruits[fruits.length - 1];
</script>

</body>
</html>

Example 4

Result View Example

Adding Array Elements

You can add new elements to an array using push() or the array's length property:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Adding Array Elements</h4>
<p id="demo"></p>

<script>
const fruits = ["Banana", "Orange"];

// push() adds to the end
fruits.push("Mango");

// Using length adds to the end
fruits[fruits.length] = "Apple";

// Adding at a higher index creates empty slots
fruits[5] = "Grape";

let text = "After adding elements: " + fruits + "<br><br>";

for (let i = 0; i < fruits.length; i++) {
  if (fruits[i] === undefined) {
    text += "Index " + i + ": (empty)" + "<br>";
  } else {
    text += "Index " + i + ": " + fruits[i] + "<br>";
  }
}

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference