where does the array elements get stored when we use shift in javascript
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
add a comment |
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
Um...you probably lefttemp.shift()
commented out.
– Zohir Salak
Nov 25 '18 at 14:28
add a comment |
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
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
javascript node.js
asked Nov 25 '18 at 13:50
Jitendra KoyandeJitendra Koyande
33
33
Um...you probably lefttemp.shift()
commented out.
– Zohir Salak
Nov 25 '18 at 14:28
add a comment |
Um...you probably lefttemp.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
add a comment |
1 Answer
1
active
oldest
votes
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)
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 dounshift
without passing any value.. it's sort of doing nothing.
– Nitish Narang
Nov 25 '18 at 14:18
So, when you say you doshift()
and thenunshift()
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
|
show 1 more comment
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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 dounshift
without passing any value.. it's sort of doing nothing.
– Nitish Narang
Nov 25 '18 at 14:18
So, when you say you doshift()
and thenunshift()
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
|
show 1 more comment
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)
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 dounshift
without passing any value.. it's sort of doing nothing.
– Nitish Narang
Nov 25 '18 at 14:18
So, when you say you doshift()
and thenunshift()
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
|
show 1 more comment
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)
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)
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 dounshift
without passing any value.. it's sort of doing nothing.
– Nitish Narang
Nov 25 '18 at 14:18
So, when you say you doshift()
and thenunshift()
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
|
show 1 more comment
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 dounshift
without passing any value.. it's sort of doing nothing.
– Nitish Narang
Nov 25 '18 at 14:18
So, when you say you doshift()
and thenunshift()
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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Um...you probably left
temp.shift()
commented out.– Zohir Salak
Nov 25 '18 at 14:28