How to Create Different Types of Arrays in JavaScript ?
In JavaScript, you can create an array using square brackets []
and optionally initialize it with values. Here are a few examples:
- Empty Array:
- Create an 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:
- Arrays in JavaScript can hold elements of different 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); - You can also use the