JavaScript Array()
The Array() Constructor
The Array() constructor creates Array objects.
Array objects are used to store multiple values in a single variable.
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 »
Explanation
| Syntax | Description |
|---|---|
| new Array() | Creates an empty array. |
| new Array(x) | Creates an array with x empty spots. |
| new Array(element1) | Creates an array with one element. |
| new Array(element1, ..., elementN) |
Creates an array with multiple elements. |
Important Warning
Arrays can also be created using an array literal [].
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.
Array Literal (Preferred)
The recommended way to create an array is with an array literal:
Object Hieraky
Array objects inherit from Object.
Object └─ Array
Array() Questions
| Question | Answer |
|---|---|
| Constructor Type | Function |
| Creates | Array objects |
| Inherits From | Object |
Browser Support
Array() is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera |