Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP W3.CSS C C++ C# HOW TO 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 INTRO TO HTML & CSS BASH RUST TOOLS

JS Tutorial

JS Home JS Introduction JS Where To JS Output JS Syntax JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Scope JS Dates JS Temporal  New JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Style Guide JS Reference JS Projects  New JS Versions JS HTML DOM JS HTML Events JS HTML First

JS Advanced

JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


The new Array() Constructor

The new Array() constructor creates an Array object.

Syntax

new Array()
new Array(number)
new Array(element1, element2, ..., elementN)

Array()
Array(number)
Array(element1, element2, ..., elementN)

Examples

new Array()
new Array(3)
new Array("3")
new Array("Saab", "Volvo", "BMW")
Try it Yourself »
Array()
Array(3)
Array("3")
Array("Saab", "Volvo", "BMW")
Try it Yourself »

Syntax Description
new Array() Creates an empty array.
new Array(x) Creates an array with x empty spots.
new Array(elem1) Creates an array with one element.
new Array(elem1,...,elemN) Creates an array with multiple elements.

The Difference? new Array() vs Array()

The short answer is: there is no functional difference between new Array() and Array().

Both commands do the exact same thing and create an array.


Note

The Array creator function is smart.

It is programmed to check if you forgot the new keyword.

If you leave it out, it adds it behind the scene.



JavaScript new Array()

JavaScript has a built-in array constructor new Array().

But you can most often use [] instead.

These two different statements both create a new empty array named points:

const points = new Array();
const points = [];

These two different statements both create a new array containing 6 numbers:

const points = new Array(40, 100, 1, 5, 25, 10);
const points = [40, 100, 1, 5, 25, 10];
Try it Yourself »

The new keyword can produce some unexpected results:

// Create an array with three elements:
const points = new Array(40, 100, 1);
Try it Yourself »
// Create an array with two elements:
const points = new Array(40, 100);
Try it Yourself »
// Create an array with one element ???
const points = new Array(40);  
Try it Yourself »

A Common Error

const points = [40];

is not the same as:

const points = new Array(40);
// Create an array with one element:
const points = [40];
Try it Yourself »
// Create an array with 40 undefined elements:
const points = new Array(40);  
Try it Yourself »

Important Warning

Use an array literal [ ] instead of the new Array() when possible.

new Array(number) is a special dangerous case.

new Array(3) creates an array with 3 empty elements; not an array containing the number 3.

Using [ ] is faster to type, easier to read, and avoids the "single number trap":

For example, [3] actually creates [3], not [null,null,null].


Array Literal (Preferred)

The recommended way to create an array is with an array literal:

Example

const cars = ["Saab", "Volvo", "BMW"];
Try it Yourself »


×

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-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->