Parameter: This method does not accept any parameter.
Return value: This method returns the removed element array, If the array is not empty, otherwise return undefined.
Example: This simple example illustrates the working of the pop() method.
Explanation: In the first line, we have created an array and called the pop method to remove the last element, & on the next line where we have printed the returned value of the pop method, one element has been removed from the array. On the last line, the original array whose length is reduced by 1.
array.splice( index, remove_count, item_list );
- index: It is a required parameter. This is an index where to start removing the elements in the array.
- remove_count: The number of elements to remove from starting index.
- items_list: The list of new items, separated by a comma, which is to be inserted from the starting index.
Return value: It returns an array, which may be empty or single-valued according to the number of elements removed.
Example: This example describes the splice method removing the last element from the array.
Explanation: In the first line, we have initialized an array and on the third line we have called the splice() method with myArray object, by providing the parameters as myArray.length-1 to start the deletion from the last index and 1 deleteCount to delete a single element. The removed element will be returned as a single-valued array.