{"id":5290,"date":"2019-12-12T12:45:15","date_gmt":"2019-12-12T07:15:15","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=5290"},"modified":"2019-12-12T14:05:06","modified_gmt":"2019-12-12T08:35:06","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 method in javascript"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">How to use shift and unshift method in javascript<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Date: 12\/12\/2019<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Introduction:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will explain how to use shift() and unshift() to add and remove the elements from the array in javascript.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">shift() used to remove the first element of the array.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">unshift()  is used to add one or more elements to the array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">shift():<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>syntax:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">arr.shift()<\/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 = [100, 289, 760, 430, 290];\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<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"485\" height=\"67\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/shift.png\" alt=\"\" class=\"wp-image-5291\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/shift.png 485w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/shift-300x41.png 300w\" sizes=\"(max-width: 485px) 100vw, 485px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>This function prints the removed element of the array<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>If the array is empty it will return <strong>undefined<\/strong><\/li><\/ul>\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 = [];\nvar value = arr.shift();\n\nconsole.log(value);\nconsole.log(arr);<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"464\" height=\"45\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/unshift.png\" alt=\"\" class=\"wp-image-5292\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/unshift.png 464w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/unshift-300x29.png 300w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">unshift() : <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>syntax<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">arr. unshift(ele1,ele2&#8230;&#8230;&#8230;)  <\/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];\nconsole.log(arr.unshift(220,560));\nconsole.log(arr);\n<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"466\" height=\"34\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/Screenshot-2019-12-12-at-12.43.04.png\" alt=\"\" class=\"wp-image-5296\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/Screenshot-2019-12-12-at-12.43.04.png 466w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/12\/Screenshot-2019-12-12-at-12.43.04-300x22.png 300w\" sizes=\"(max-width: 466px) 100vw, 466px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for using&nbsp;<a href=\"https:\/\/pheonixsolutions.com\/\">pheonix solutions<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to use shift and unshift method in javascript Date: 12\/12\/2019 Introduction: In this article, we will explain how to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"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_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","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":"","_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":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5290\/revisions"}],"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}]}}