Skip to main content

Command Palette

Search for a command to run...

JavaScript Quiz ⚑ push() & shift() array methods

Updated
β€’1 min read
JavaScript Quiz ⚑ push() & shift() array methods

Let's break it down πŸ”₯

  1. shift() removes the first element and returns the removed element.

  2. push() adds the elements at the end of the array and returns the new length of the array

const roles = ["DevOps", "Front-End", "Back-End", "Designer"];

πŸ‘‰ The current array length is 4.

roles.shift() Removing DevOps element from the array

πŸ‘‰ Now, array length becomes 3 because one element has been removed with the shift method.

roles.push() is adding the deleted DevOps element at the end of the array.

πŸ‘‰ After pushing "DevOps" back to the end of the array, the new length of the array will return to the last push method.

roles.push() At the end length, 4 will get pushed as an element at the end of the array.

So, the output will become:

['Front-End', 'Back-End', 'Designer', 'DevOps', 4]
189 views

More from this blog

Code With Shan

6 posts

Full-Stack Web Developer ~ Technical Writer πŸ’»

I share (CSS | JavaScript | React | Web3.0) Code Snippets with β™₯