Array declaration
Declaring an array means creating a variable that holds an ordered list of values, such as a list of names, prices or tasks. JavaScript offers a few ways to create one, from the everyday array literal to helpers like Array.from(). Choosing the right one avoids surprising results, especially with the Array constructor.
The array literal
Square brackets are the standard way to declare an array. Values are separated by commas, and the list can be empty, can hold any type, and can even mix types.
const fruits = ['apple', 'banana', 'mango'];
const empty = [];
const mixed = [42, 'text', true, null, { id: 1 }, [1, 2]];
const multiline = [
'Monday',
'Tuesday',
'Wednesday', // a trailing comma is allowed and ignored
];
console.log(fruits.length); // 3
console.log(fruits[0]); // 'apple'
const grid = [
[1, 2, 3],
[4, 5, 6],
];
console.log(grid[1][2]); // 6
Indexes start at 0, so the last item is at length - 1. Arrays inside arrays create a grid, which is how tables of data or game boards are often stored. Mixing many unrelated types is legal but usually a sign the data would be clearer as an object.
Other ways to create arrays
| Code | Result | Use it for |
|---|---|---|
new Array(3) | Length 3, but no actual items (empty slots) | Rarely needed |
new Array(3, 4) | [3, 4] | Rarely needed |
Array.of(3) | [3] | Building an array from arguments safely |
Array.from('abc') | ['a', 'b', 'c'] | Converting strings, Sets, NodeLists |
Array.from({ length: 5 }, (_, i) => i * 2) | [0, 2, 4, 6, 8] | Generating a sequence |
new Array(4).fill(0) | [0, 0, 0, 0] | Pre-filled arrays |
[...set] | An array of the set's values | Copying or converting iterables |
Why new Array(n) surprises people
The Array constructor behaves differently depending on its arguments. With a single number it creates an array of that length with empty slots, not an array containing that number. With anything else it creates an array of those values. So new Array(5) and new Array('5') give completely different results.
Empty slots are also odd. Methods like map and forEach skip them, so new Array(3).map(() => 'x') still has three empty slots instead of three 'x' values. If you need a sized array, use Array.from({ length: n }, ...) or new Array(n).fill(value). One more trap with fill: new Array(3).fill([]) puts the same inner array in all three positions, so pushing into one changes all of them. Use Array.from with a function that returns a new array each time.
const does not freeze an array
Most arrays are declared with const. This prevents you from assigning a new array to the variable, but it does not stop you from changing the contents. fruits.push('kiwi') and fruits[0] = 'pear' both work. Writing fruits = [] throws a TypeError. If you truly need an unchangeable array, Object.freeze(fruits) blocks changes, though only at the top level. Use let only when you plan to reassign the variable to a different array.
Quick summary
- Use
[]literals for almost everything. - Use
Array.from()to convert or generate, andArray.of()instead of the constructor. - Avoid
new Array(n)unless you immediately fill it. constprotects the variable, not the items inside.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.