Javascript Creating arrays
In JavaScript, an array is a collection of elements of any data type that can be accessed by a numeric index. Arrays can be created in several ways:
Using an array literal:
let myArray = [1, 2, 3, 4, 5];
Using the Array() constructor:
let myArray = new Array(1, 2, 3, 4, 5);
Creating an empty array and populating it later:
let myArray = [];
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
Arrays can contain elements of any data type, including other arrays. Array elements can be accessed and modified using their index.
let myArray = ['apple', 'banana', 'orange'];
console.log(myArray[0]); // Output: 'apple'
myArray[1] = 'pear';
console.log(myArray); // Output: ['apple', 'pear', 'orange']
Arrays also have a number of built-in methods, such as push(), pop(), shift(), and unshift(), that can be used to add or remove elements from the array.