where does the array elements get stored when we use shift in javascript












-1















I tried array.shift() and array.unshift().
As a beginner in javascript I am not able to understand if use shift(), first array element will get removed but when we use unshift after shift from where does the removed element get added?



var temp = ['Five','Four','Three']
// temp.shift()
temp.unshift()
console.log(`we have ${temp}`)


This is a sample snippet










share|improve this question























  • Um...you probably left temp.shift() commented out.

    – Zohir Salak
    Nov 25 '18 at 14:28


















-1















I tried array.shift() and array.unshift().
As a beginner in javascript I am not able to understand if use shift(), first array element will get removed but when we use unshift after shift from where does the removed element get added?



var temp = ['Five','Four','Three']
// temp.shift()
temp.unshift()
console.log(`we have ${temp}`)


This is a sample snippet










share|improve this question























  • Um...you probably left temp.shift() commented out.

    – Zohir Salak
    Nov 25 '18 at 14:28
















-1












-1








-1








I tried array.shift() and array.unshift().
As a beginner in javascript I am not able to understand if use shift(), first array element will get removed but when we use unshift after shift from where does the removed element get added?



var temp = ['Five','Four','Three']
// temp.shift()
temp.unshift()
console.log(`we have ${temp}`)


This is a sample snippet










share|improve this question














I tried array.shift() and array.unshift().
As a beginner in javascript I am not able to understand if use shift(), first array element will get removed but when we use unshift after shift from where does the removed element get added?



var temp = ['Five','Four','Three']
// temp.shift()
temp.unshift()
console.log(`we have ${temp}`)


This is a sample snippet







javascript node.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 13:50









Jitendra KoyandeJitendra Koyande

33




33













  • Um...you probably left temp.shift() commented out.

    – Zohir Salak
    Nov 25 '18 at 14:28





















  • Um...you probably left temp.shift() commented out.

    – Zohir Salak
    Nov 25 '18 at 14:28



















Um...you probably left temp.shift() commented out.

– Zohir Salak
Nov 25 '18 at 14:28







Um...you probably left temp.shift() commented out.

– Zohir Salak
Nov 25 '18 at 14:28














1 Answer
1






active

oldest

votes


















2














Array.unshift adds the element to first position in the array. Pls see below example.



Always better to read doc as well






let arr = ['Five','Four','Three']

arr.unshift('add1')

console.log(arr)





Also, Array.shift removes first element from the array






let arr1 = ['Five','Four','Three']

arr1.shift()

console.log(arr1)





UPDATE - Doing shift and unshift parallelly to show unshift() without passing any value does nothing.






let arr1 = ['Five','Four','Three']

arr1.shift()

arr1.unshift()

console.log(arr1)








share|improve this answer


























  • If i use shift first on array consisting 5 element it will remove first element. But after shift() If I call unshift() without any parameter it retrieves the removed element. My question is from where does it retrieve the deleted values?

    – Jitendra Koyande
    Nov 25 '18 at 14:15











  • It never happens. If you do unshift without passing any value.. it's sort of doing nothing.

    – Nitish Narang
    Nov 25 '18 at 14:18











  • So, when you say you do shift() and then unshift() your value comes back.. it doesn't happen.. I will update answer for you

    – Nitish Narang
    Nov 25 '18 at 14:18











  • @JitendraKoyande Updated

    – Nitish Narang
    Nov 25 '18 at 14:20






  • 1





    Got it. Thanks for the quick reply. @NishantNarang

    – Jitendra Koyande
    Nov 25 '18 at 14:27













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468151%2fwhere-does-the-array-elements-get-stored-when-we-use-shift-in-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Array.unshift adds the element to first position in the array. Pls see below example.



Always better to read doc as well






let arr = ['Five','Four','Three']

arr.unshift('add1')

console.log(arr)





Also, Array.shift removes first element from the array






let arr1 = ['Five','Four','Three']

arr1.shift()

console.log(arr1)





UPDATE - Doing shift and unshift parallelly to show unshift() without passing any value does nothing.






let arr1 = ['Five','Four','Three']

arr1.shift()

arr1.unshift()

console.log(arr1)








share|improve this answer


























  • If i use shift first on array consisting 5 element it will remove first element. But after shift() If I call unshift() without any parameter it retrieves the removed element. My question is from where does it retrieve the deleted values?

    – Jitendra Koyande
    Nov 25 '18 at 14:15











  • It never happens. If you do unshift without passing any value.. it's sort of doing nothing.

    – Nitish Narang
    Nov 25 '18 at 14:18











  • So, when you say you do shift() and then unshift() your value comes back.. it doesn't happen.. I will update answer for you

    – Nitish Narang
    Nov 25 '18 at 14:18











  • @JitendraKoyande Updated

    – Nitish Narang
    Nov 25 '18 at 14:20






  • 1





    Got it. Thanks for the quick reply. @NishantNarang

    – Jitendra Koyande
    Nov 25 '18 at 14:27


















2














Array.unshift adds the element to first position in the array. Pls see below example.



Always better to read doc as well






let arr = ['Five','Four','Three']

arr.unshift('add1')

console.log(arr)





Also, Array.shift removes first element from the array






let arr1 = ['Five','Four','Three']

arr1.shift()

console.log(arr1)





UPDATE - Doing shift and unshift parallelly to show unshift() without passing any value does nothing.






let arr1 = ['Five','Four','Three']

arr1.shift()

arr1.unshift()

console.log(arr1)








share|improve this answer


























  • If i use shift first on array consisting 5 element it will remove first element. But after shift() If I call unshift() without any parameter it retrieves the removed element. My question is from where does it retrieve the deleted values?

    – Jitendra Koyande
    Nov 25 '18 at 14:15











  • It never happens. If you do unshift without passing any value.. it's sort of doing nothing.

    – Nitish Narang
    Nov 25 '18 at 14:18











  • So, when you say you do shift() and then unshift() your value comes back.. it doesn't happen.. I will update answer for you

    – Nitish Narang
    Nov 25 '18 at 14:18











  • @JitendraKoyande Updated

    – Nitish Narang
    Nov 25 '18 at 14:20






  • 1





    Got it. Thanks for the quick reply. @NishantNarang

    – Jitendra Koyande
    Nov 25 '18 at 14:27
















2












2








2







Array.unshift adds the element to first position in the array. Pls see below example.



Always better to read doc as well






let arr = ['Five','Four','Three']

arr.unshift('add1')

console.log(arr)





Also, Array.shift removes first element from the array






let arr1 = ['Five','Four','Three']

arr1.shift()

console.log(arr1)





UPDATE - Doing shift and unshift parallelly to show unshift() without passing any value does nothing.






let arr1 = ['Five','Four','Three']

arr1.shift()

arr1.unshift()

console.log(arr1)








share|improve this answer















Array.unshift adds the element to first position in the array. Pls see below example.



Always better to read doc as well






let arr = ['Five','Four','Three']

arr.unshift('add1')

console.log(arr)





Also, Array.shift removes first element from the array






let arr1 = ['Five','Four','Three']

arr1.shift()

console.log(arr1)





UPDATE - Doing shift and unshift parallelly to show unshift() without passing any value does nothing.






let arr1 = ['Five','Four','Three']

arr1.shift()

arr1.unshift()

console.log(arr1)








let arr = ['Five','Four','Three']

arr.unshift('add1')

console.log(arr)





let arr = ['Five','Four','Three']

arr.unshift('add1')

console.log(arr)





let arr1 = ['Five','Four','Three']

arr1.shift()

console.log(arr1)





let arr1 = ['Five','Four','Three']

arr1.shift()

console.log(arr1)





let arr1 = ['Five','Four','Three']

arr1.shift()

arr1.unshift()

console.log(arr1)





let arr1 = ['Five','Four','Three']

arr1.shift()

arr1.unshift()

console.log(arr1)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 14:20

























answered Nov 25 '18 at 13:52









Nitish NarangNitish Narang

2,9601815




2,9601815













  • If i use shift first on array consisting 5 element it will remove first element. But after shift() If I call unshift() without any parameter it retrieves the removed element. My question is from where does it retrieve the deleted values?

    – Jitendra Koyande
    Nov 25 '18 at 14:15











  • It never happens. If you do unshift without passing any value.. it's sort of doing nothing.

    – Nitish Narang
    Nov 25 '18 at 14:18











  • So, when you say you do shift() and then unshift() your value comes back.. it doesn't happen.. I will update answer for you

    – Nitish Narang
    Nov 25 '18 at 14:18











  • @JitendraKoyande Updated

    – Nitish Narang
    Nov 25 '18 at 14:20






  • 1





    Got it. Thanks for the quick reply. @NishantNarang

    – Jitendra Koyande
    Nov 25 '18 at 14:27





















  • If i use shift first on array consisting 5 element it will remove first element. But after shift() If I call unshift() without any parameter it retrieves the removed element. My question is from where does it retrieve the deleted values?

    – Jitendra Koyande
    Nov 25 '18 at 14:15











  • It never happens. If you do unshift without passing any value.. it's sort of doing nothing.

    – Nitish Narang
    Nov 25 '18 at 14:18











  • So, when you say you do shift() and then unshift() your value comes back.. it doesn't happen.. I will update answer for you

    – Nitish Narang
    Nov 25 '18 at 14:18











  • @JitendraKoyande Updated

    – Nitish Narang
    Nov 25 '18 at 14:20






  • 1





    Got it. Thanks for the quick reply. @NishantNarang

    – Jitendra Koyande
    Nov 25 '18 at 14:27



















If i use shift first on array consisting 5 element it will remove first element. But after shift() If I call unshift() without any parameter it retrieves the removed element. My question is from where does it retrieve the deleted values?

– Jitendra Koyande
Nov 25 '18 at 14:15





If i use shift first on array consisting 5 element it will remove first element. But after shift() If I call unshift() without any parameter it retrieves the removed element. My question is from where does it retrieve the deleted values?

– Jitendra Koyande
Nov 25 '18 at 14:15













It never happens. If you do unshift without passing any value.. it's sort of doing nothing.

– Nitish Narang
Nov 25 '18 at 14:18





It never happens. If you do unshift without passing any value.. it's sort of doing nothing.

– Nitish Narang
Nov 25 '18 at 14:18













So, when you say you do shift() and then unshift() your value comes back.. it doesn't happen.. I will update answer for you

– Nitish Narang
Nov 25 '18 at 14:18





So, when you say you do shift() and then unshift() your value comes back.. it doesn't happen.. I will update answer for you

– Nitish Narang
Nov 25 '18 at 14:18













@JitendraKoyande Updated

– Nitish Narang
Nov 25 '18 at 14:20





@JitendraKoyande Updated

– Nitish Narang
Nov 25 '18 at 14:20




1




1





Got it. Thanks for the quick reply. @NishantNarang

– Jitendra Koyande
Nov 25 '18 at 14:27







Got it. Thanks for the quick reply. @NishantNarang

– Jitendra Koyande
Nov 25 '18 at 14:27






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468151%2fwhere-does-the-array-elements-get-stored-when-we-use-shift-in-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'