let emptyArray = ; let arrayWithValues = [1, 2, 3];
Once you have an array, you can access and modify its elements using indexing. Indexing starts at 0, so the first element of an array will have an index of 0. For example, to access the first element of the arrayWithValues array created above, you would use
console.log(arrayWithValues) // 1
You can also use the array’s length property to find the number of elements in the array. For example, to find the length of the arrayWithValues array, you would use
console.log(arrayWithValues.length) // 3
Arrays also have a variety of methods that you can use to work with the data in the array. For example, the
push() method adds a new element to the end of the array, and the
pop() method removes the last element from the array.
var arr = [1, 2, 3]; arr.push(4); console.log(arr); // [1, 2, 3, 4]
var arr = [1, 2, 3]; arr.pop(); console.log(arr); // [1, 2]
You can also use the
sort() method to sort the elements of an array in ascending or descending order, and the
reverse() method to reverse the order of the elements in the array.
var arr = [1, 2, 3]; arr.sort(); console.log(arr); // [1, 2, 3]
var arr = [1, 2, 3]; arr.reverse(); console.log(arr); // [3, 2, 1]