What is an Array?
Creating an Array
 and can hold any data type, including numbers, strings, objects, and even other arrays. To create an array, you simply declare a new variable and assign it to an array using the square brackets.
Here, arrayName is the name of the array, and value1, value2, and value3 are the values that we want to store in the array. For example:
This creates an array called fruits that holds three strings: ‘apple’, ‘banana’, and ‘orange’.
We can also create an empty array and add values to it later:
This creates an empty array called numbers and adds the numbers 1, 2, and 3 to it using the push() method.
Accessing Array Elements
This would output ‘apple’ to the console. We can access the second and third elements in the same way:
Modifying Array Elements
We can modify the values in an array by accessing them using their index and assigning a new value to them. For example, to change the second element in the fruits array to ‘pear’, we can use the following code:
This would change the second element in the fruits array to ‘pear’.
push()method adds one or more elements to the end of an array and returns the new length of the array.
pop()method removes the last element from an array and returns that element.
shift()method shifts the all elements from right to left. Which removes the first element from an array:
unshift()method adds one or more elements to the beginning of an array and returns the new length of the array.
splice()method can add, remove, and/or replace elements in an array. It takes in at least two arguments: the index at which to start making changes, and the number of elements to remove. Additional arguments can be provided to add elements at the same index. The method returns an array of the removed elements, or an empty array if no elements were removed.
splice()method inserts the string ‘grape’ into the fruits array at index 1, pushing the original element at index 1 and subsequent elements back by one index.
slice()method returns a new array containing a portion of an existing array. The portion is specified by the starting and ending indexes, which are passed as arguments to the method. The starting index is inclusive, and the ending index is exclusive. If no arguments are passed, the method returns a copy of the entire array. The original array is not modified.
slice()method creates a new array called `newArray` that contains the elements of the fruits array from index 1 up to, but not including, index 3.
indexOf()method returns the index of the first occurrence of a specified element in an array:
Here, the indexOf() method returns the index of the string ‘pear’ in the fruits array, which is 1.