In JavaScript, you can create an array using square brackets [] and optionally initialize it with values. Here are a few examples:
- Empty Array:
let emptyArray = [];
- Array with Values:
- Initialize an array with values.
let numbers = [1, 2, 3, 4, 5]; let fruits = ["Apple", "Banana", "Orange"];
- Mixed Data Types:
let mixedArray = [1, "two", true, { key: "value" }];
- Using the
Array Constructor:
- You can also use the
Array constructor to create an array.
let newArray = new Array(); let arrayWithValues = new Array(1, 2, 3, 4, 5);
|