Creating and working with Arrays in JS

Creating and working with Arrays in JS

There are several ways to create arrays in Javascript. Also, adding new index values will update the number of items in the array.  Length can be set to update the number of items in the array.  To loop through the items in an array you can use for loop, although this will also return the values that are empty, or you can use forEach which only returns values that are set regardless of the number of blank items in the array.

const arr1 = ['one','two','three'];
const arr2 = new Array('one','two','three');
const arr3 = Array('one','two','three');
const arr4 = Array(20);
const arr5 = [20];
 
arr1.length = 20;
console.log(arr4.length);
console.log(arr4['length']);
console.log(arr2[arr2.length-1]);
console.log(arr2[2]);
 
console.log(arr1);
console.log(arr2);
console.log(arr3);
console.log(arr4);
console.log(arr5);
 
arr1[100] = 'test';
 
for(let i=0;i<arr1.length;i++){
   console.log(arr1[i]);
}
 
arr1.forEach(ele => {
   console.log(ele);
})