Home > other >  How we have a return value of pop() function in JavaScript?
How we have a return value of pop() function in JavaScript?

Time:11-18

While I was recently studying JS coming from a C background, I found out that the pop() function of arrays has a return value in JS.

Now, in C we don't have a return value for pop_back() method because according to a paper by Cargill, it is impossible to design an exception-safe stack pop function as answered here

So how are things working in JS or am I missing something?

CodePudding user response:

None of the "Exceptions Thrown by T" cases pop up in JavaScript, because there simply is no assignment or construction operator invoked. JavaScript is a garbage-collected language, so everything is a handle to the actual object. Calling pop just shrinks the array by one and returns the handle that was there without doing anything more.

If you want to reason about it in terms of C , everything is a std::shared_ptr<T>, so pop just move-constructs that to its return value, which is exception-free.

  • Related