how do i create an array in javascript in javascript

To create an array in JavaScript, you can use the array literal syntax or the array constructor. Here's an example of creating an array using both methods:

  1. Array Literal Syntax:
index.tsx
// Empty Array
let emptyArray = [];

// Array with initial values
let numberArray = [1, 2, 3, 4, 5];
let stringArray = ['apple', 'banana', 'orange'];
150 chars
7 lines
  1. Array Constructor Syntax:
index.tsx
// Empty Array
let emptyArray = new Array();

// Array with initial values
let numberArray = new Array(1, 2, 3, 4, 5);
let stringArray = new Array('apple', 'banana', 'orange');
177 chars
7 lines

Both methods will create an array in JavaScript that you can then manipulate and access its elements. Note that arrays in JavaScript can store different types of data, such as numbers, strings, objects, or even other arrays.

related categories

gistlibby LogSnag