You have two main ways to create empty array: const myArray=[] or const myArray = new Array();
You have two main ways to go: simple declaration with square brackets.
const myArray = []
Or instantiation of the Array Object using the constructor method:
const myArray = new Array()
The trendy kids favor the first way, nowadays, with the empty square brackets, but if you have really cool ring tones on your phone, you can probably still get away with the latter syntax.
So, why const?
For arrays, do not declare using var, since that can be mutated into another datatype. Use const instead. The third flavor, let, allows the datatype to be changed, which you probably don’t want to do with an array.
const, is, as the name implies, a constant. However, unlike the name implies,
a const array is only kinda, sorta constant. While you cannot mutate the datatype of a const array, such as change it to a string, you can nevertheless add and remove items. The following demonstrates that our new empty array is constant in datatype, but not in content:
const fruits = []; fruits = "citrus"; // throws error — cannot mutate (array to string) fruits[0] = "Apricot"; // add item to index 0 (first position) fruits = 12; // throws error — cannot mutate (array to number) fruits.push("Banana"); // add item to end of array const fruits = []; // throws error — cannot re-declare a const fruits.unshift("Apple") // add item to beginning of array