In this blog we are going to learn most useful methods of arrays in javaScript. Javascript provides multiple built-in methods for common tasks so, that se don't need to write the logic from scratch.
push() and pop() : Both function perform very simple and common task.
push() : From the name you can guess it is used to push(add) new values in array at the end of the array and return new array length.
const fruits = [ ' apple ' , ' banana ' ]; const newLength = fruits . push ( ' orange ' , ' mango ' );
console . log ( fruits ); // ["apple", "banana", "orange", "mango"] console . log ( newLength ); // 4
Enter fullscreen mode
Exit fullscreen mode
pop() : This method remove the last element from the array from original array.
it returns the removed element.
const fruits = [ ' apple ' , ' banana ' , ' cherry ' ]; const lastFruit = fruits . pop ();
console . log ( fruits ); // ["apple", "banana"] (original array modified) console .β¦