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
Use array literals [ ] instead of the Array() constructor when possible.
new Array(3) creates an empty array with length 3, not an array containing the number 3.
Arrays are normally created with [].
new Array(number) is the special dangerous case.
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 |