Creating Arrays with Array Literals
You'll learn how to create arrays using the simplest and most common syntax: square brackets with comma-separated values.
What Is an Array Literal?
An array literal is the straightforward way to create an array in JavaScript. You use square brackets [] and place values inside, separated by commas. Think of it like writing a shopping list directly on paper—you simply list the items in order, one after another.
Here's the basic syntax:
const emptyArray = [];
const numbers = [1, 2, 3, 4, 5];
const colors = ["red", "green", "blue"];
const mixed = [42, "hello", true];
Why Use Array Literals?
Array literals are:
- Readable: You can see exactly what's in the array at a glance
- Concise: No extra ceremony or function calls needed
- Flexible: Arrays can hold any type of value, including other arrays or objects
Examples
// An array of names
const students = ["Alice", "Bob", "Charlie"];
// An array with different types
const data = [100, "complete", true, "success"];
// Nested arrays (arrays inside arrays)
const matrix = [[1, 2], [3, 4], [5, 6]];
// Empty array, ready to be filled later
const results = [];
You can even mix objects you've created with your own constructor functions or classes into arrays—they're just values like any other.
Key Takeaway: Array literals use square brackets [] with comma-separated values to create arrays quickly and clearly—it's the standard way to initialize arrays in JavaScript.