0

What is the internal code logic for Push and Pop methods in javascript..?? How does Push method stores the values in Array.

asked Dec 9, 2014 at 18:33
2
  • 2
    There are as many answers as there are JavaScript runtimes. Commented Dec 9, 2014 at 18:34
  • 3
    It will differ for every JavaScript engine out there. You'll have to look at each one's source code (not always available) to see how it works. For example, here's the latest version of Google's V8 ArrayPush() internal method. Commented Dec 9, 2014 at 18:35

2 Answers 2

7

The push and pop methods are intentionally generic, they only rely on the existance of a length property, and that they can add and remove properties.

The push method will read the length property, add a property with that name, and increase the length. Basically:

function push(value) {
 var len = this.length;
 this[len] = value;
 len++;
 this.length = len;
 return len;
}

The pop method will read the length property, decrease it, get the property with that name and remove the property. Basically:

function pop() {
 var len = this.length - 1;
 var value = this[len];
 this.length = len;
 delete this[len];
 return value;
}

The actual implementations are a bit more complex, as they support for example multiple parameters for the push method, and some more error checks. There might also implement special optimised code for when the object is actually an array, but then the generic code is still there for other objects.

The methods are intentionally generic so that they can be used on objects that aren't actually arrays. You can make your own object that supports them by just having a length property:

var o = {
 length: 0,
 push: Array.prototype.push,
 pop: Array.prototype.pop
};
o.push(1);
var one = o.pop();

Demo: http://jsfiddle.net/Guffa/9r4gavzb/

answered Dec 9, 2014 at 18:50

2 Comments

Thanks for you detailed answer Appreciate it.!! My Goal was to create a customArray() Object like clone of Array(). Now after pushing all my elements how the linking happens between each Object i have pushed into myCustomArray(). "var values = new CustomArray('xyz','xxx','yzx');" like how the values are stored inside.
@dcheepurapalli: Items in an array are stored as properties in the object. The first item is just a property with the name "0", the second item just a property with the name "1" and so on. That's why the push and pop methods will happily use an object instead of an array as long as it has a length property.
0

We can try some tests and test behavior:

const arr1 = []
const { push: push1 } = arr
const arr2 = []
const { push: push2 } = arr
console.log(push1 === push2) // true
console.log(push1 === Array.prototype.push) // true
push1(1) // TypeError: Cannot convert undefined or null to object
push1.call(arr1, 1) // arr1: [1], arr2: []
push2.call(arr1, 2) // arr1: [1, 2], arr2: []
push1.bind(arr2)(1) // arr1: [1, 2], arr2: [1]
push.call(arr2, 2)

And we can say that push method uses this under the hood...

answered Jul 3, 2021 at 14:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.