JavaScript Array Constructor

Back to JS page

Table of Contents

new Array() vs Array()

There is no difference between new Array() and Array(). Both create a new array:

<!DOCTYPE html>
<html>
<body>

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

<h4>new Array() vs Array()</h4>
<p id="demo"></p>

<script>
// new Array() - creates a new array
const arr1 = new Array("Apple", "Banana", "Orange");

// Array() - same result, no 'new' needed
const arr2 = Array("Apple", "Banana", "Orange");

document.getElementById("demo").innerHTML =
  "new Array(...): " + arr1 + "<br>" +
  "Array(...): " + arr2 + "<br><br>" +
  "Both give the same result!";
</script>

</body>
</html>

Example 1

Result View Example

A Common Error

A common error is using new Array(number) with a single numeric argument. It creates an array with that many undefined elements, NOT an array with one element:

<!DOCTYPE html>
<html>
<body>

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

<h4>A Common Error</h4>
<p id="demo"></p>

<script>
// WARNING: new Array(5) creates an array with 5 empty slots!
const arr = new Array(5);

let text = "new Array(5):<br>";
text += "Length: " + arr.length + "<br>";
text += "Elements: " + arr + "<br><br>";

text += "This creates 5 EMPTY slots, not an array with value 5!<br><br>";

// To create an array with one element use brackets
const correct = [5];
text += "Correct way [5]: length = " + correct.length + ", value = " + correct[0];

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

</body>
</html>

Example 2

Result View Example

Array Literal (Preferred)

The array literal [] is the preferred way to create an array. It is simpler and safer:

<!DOCTYPE html>
<html>
<body>

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

<h4>Array Literal (Preferred)</h4>
<p id="demo"></p>

<script>
// Preferred: array literal
const fruits = ["Apple", "Banana", "Orange"];

// Using new Array() with explicit elements (works but not preferred)
const cars = new Array("Volvo", "BMW", "Toyota");

let text = "Array literal: " + fruits + "<br>";
text += "new Array(): " + cars + "<br><br>";
text += "<b>Why use the array literal?</b><br>";
text += "1. Simpler to read and write<br>";
text += "2. Avoids the new Array(number) error<br>";
text += "3. Better performance<br>";
text += "4. Consistent behavior";

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

</body>
</html>

Example 3

Result View Example

Document

Document in project

You can Download PDF file.

Reference