{"id":5290,"date":"2019-12-12T12:45:15","date_gmt":"2019-12-12T07:15:15","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=5290"},"modified":"2026-11-16T13:05:43","modified_gmt":"2026-11-16T07:35:43","slug":"how-to-use-shift-and-unshift-method-in-javascript","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-use-shift-and-unshift-method-in-javascript\/","title":{"rendered":"How to Use shift() and unshift() Methods in JavaScript"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Post Date:<\/strong> December 12, 2019<br><strong>Last Updated:<\/strong> November 8, 2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The JavaScript shift and unshift methods<\/strong> are commonly used to remove and add elements at the beginning of an array. These methods help developers manage array elements dynamically and modify existing arrays without creating new ones.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>shift()<\/code> method removes the first element from an array and returns the removed value, whereas the <code>unshift()<\/code> method adds one or more elements at the beginning and returns the updated array length.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will explore how to use JavaScript <code>shift()<\/code> and <code>unshift()<\/code> methods with simple examples and understand their differences.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. JavaScript shift() Method<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The JavaScript <code>shift()<\/code> method removes the first element from an array and returns the removed element.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It also changes the original array by removing the first element.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">array.shift();<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Using the shift() Method<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s create an array and remove its first element using the <code>shift()<\/code> method.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var arr = [100, 289, 760, 430, 290];\n\nvar value = arr.shift();\n\nconsole.log(value);\nconsole.log(arr);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">100\n[289, 760, 430, 290]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>arr<\/code> variable contains five elements.<\/li>\n\n\n\n<li>The <code>shift()<\/code> method removes the first element, which is <code>100<\/code>.<\/li>\n\n\n\n<li>The removed element is stored in the <code>value<\/code> variable.<\/li>\n\n\n\n<li>The updated array contains the remaining four elements.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Using shift() on an Empty Array<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If the array is empty, the <code>shift()<\/code> method returns <code>undefined<\/code> because there is no element to remove.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var arr = [];\n\nvar value = arr.shift();\n\nconsole.log(value);\nconsole.log(arr);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">undefined\n[]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The array is empty.<\/li>\n\n\n\n<li>The <code>shift()<\/code> method does not find any element to remove.<\/li>\n\n\n\n<li>It returns <code>undefined<\/code>.<\/li>\n\n\n\n<li>The original array remains empty.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. JavaScript unshift() Method<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The JavaScript <code>unshift()<\/code> method adds one or more elements to the beginning of an array.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It modifies the original array and returns the new length of the array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">array.unshift(element1, element2, ...);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can pass one or multiple elements as arguments.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Using the unshift() Method<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s create an array and add two new elements at the beginning using the <code>unshift()<\/code> method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var arr = [838, 90, 320, 410];\n\nconsole.log(arr.unshift(220, 560));\n\nconsole.log(arr);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">6\n[220, 560, 838, 90, 320, 410]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The original array contains four elements.<\/li>\n\n\n\n<li>The <code>unshift()<\/code> method adds <code>220<\/code> and <code>560<\/code> at the beginning.<\/li>\n\n\n\n<li>The method returns <code>6<\/code>, which is the new length of the array.<\/li>\n\n\n\n<li>The updated array contains six elements.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. Difference Between shift() and unshift() in JavaScript<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both methods modify the original array, but they perform different operations.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Feature<\/th><th>shift()<\/th><th>unshift()<\/th><\/tr><tr><td>Purpose<\/td><td>Removes the first element<\/td><td>Adds elements at the beginning<\/td><\/tr><tr><td>Arguments<\/td><td>Does not require arguments<\/td><td>Accepts one or more elements<\/td><\/tr><tr><td>Return value<\/td><td>Removed element<\/td><td>New array length<\/td><\/tr><tr><td>Original array<\/td><td>Modified<\/td><td>Modified<\/td><\/tr><tr><td>Empty array<\/td><td>Returns <code>undefined<\/code><\/td><td>Adds elements if provided<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">4. Practical Example: Using shift() and unshift() Together<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can use both methods to remove and add elements at the beginning of the same array.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var fruits = [\"Apple\", \"Banana\", \"Orange\"];\n\nvar removedFruit = fruits.shift();\n\nconsole.log(\"Removed Fruit:\", removedFruit);\nconsole.log(\"Updated Array:\", fruits);\n\nvar newLength = fruits.unshift(\"Mango\", \"Grapes\");\n\nconsole.log(\"New Array Length:\", newLength);\nconsole.log(\"Final Array:\", fruits);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Removed Fruit: Apple\nUpdated Array: [\"Banana\", \"Orange\"]\nNew Array Length: 4\nFinal Array: [\"Mango\", \"Grapes\", \"Banana\", \"Orange\"]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, the <code>shift()<\/code> method removes <code>Apple<\/code> from the beginning of the array. Next, the <code>unshift()<\/code> method adds <code>Mango<\/code> and <code>Grapes<\/code> to the beginning of the updated array.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This example demonstrates how both methods can be used to manage array elements dynamically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The JavaScript <code>shift()<\/code> and <code>unshift()<\/code> methods are useful for managing elements at the beginning of an array.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>shift()<\/code> method removes the first element and returns the removed value, whereas the <code>unshift()<\/code> method adds one or more elements at the beginning and returns the updated array length.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding these methods helps developers perform array operations efficiently when building JavaScript applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is the use of the shift() method in JavaScript?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>shift()<\/code> method removes the first element from an array and returns the removed element. It also modifies the original array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. What is the use of the unshift() method in JavaScript?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>unshift()<\/code> method adds one or more elements to the beginning of an array and returns the new length of the array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. What does shift() return when an array is empty?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When the array is empty, the <code>shift()<\/code> method returns <code>undefined<\/code> because there is no element to remove.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Can we add multiple elements using the unshift() method?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. The <code>unshift()<\/code> method accepts multiple arguments and adds all the specified elements to the beginning of the array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Do shift() and unshift() modify the original array?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Both methods modify the original array. The <code>shift()<\/code> method removes the first element, while the <code>unshift()<\/code> method adds elements at the beginning.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-validate-email-id-in-javascript\/\">Validate email in JavaScript<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Official Documentation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/shift\" target=\"_blank\" rel=\"noopener\">MDN \u2013 JavaScript Array shift()<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/unshift\" target=\"_blank\" rel=\"noopener\">MDN \u2013 JavaScript Array unshift()<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Indexed_collections\" target=\"_blank\" rel=\"noopener\">MDN \u2013 JavaScript Arrays<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to Our Experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for reliable JavaScript development and web application solutions?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/web-development\/\"><strong>Pheonix Solutions<\/strong><\/a> provides web development, application development, cloud solutions, and DevOps services to help businesses build efficient and scalable digital solutions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Our experts can help you develop interactive web applications, implement modern JavaScript functionality, and improve your application performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/contact\"><strong>Contact our experts<\/strong><\/a> to discuss your project requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Thank you for choosing Pheonix Solutions!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: December 12, 2019Last Updated: November 8, 2026 Introduction The JavaScript shift and unshift methods are commonly used to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13499,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[737],"class_list":["post-5290","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-architecture","tag-javascript-shift-unshift","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1nk","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/JavaScript-Shift-and-Unshift-Methods.png","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=5290"}],"version-history":[{"count":6,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5290\/revisions"}],"predecessor-version":[{"id":13502,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5290\/revisions\/13502"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media\/13499"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=5290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=5290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=5290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}