create an array in javascript

To create an array in javascript, you can use the following syntax:

index.tsx
var myArray = [element1, element2, ..., elementN];
51 chars
2 lines

You can also create an empty array and add elements to it later using the push() method:

index.tsx
var myArray = [];
myArray.push(element1);
myArray.push(element2);
...
myArray.push(elementN);
94 chars
6 lines

Another way to create an array is by using the Array() constructor:

index.tsx
var myArray = new Array(element1, element2, ..., elementN);
60 chars
2 lines

However, it is generally recommended to use the first method, as the second method can lead to unexpected results when passing a single argument that is a number.

gistlibby LogSnag