This is more of a guide to implementing this specific data structure in JavaScript, as a means of teaching the concept of a stack. It's true that arrays can do all that stacks can do and more, so in most real-world situations, you'd probably want to use an array, unless you specifically want to prevent the use of methods other than .push() and pop(). Beyond that, this is also useful as a thought experiment of the type you might be asked in a technical interview.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Doesn't JavaScript already have stack in Array,
Array.push
andArray.pop
are exactly the same functions, why create a new stack?This is more of a guide to implementing this specific data structure in JavaScript, as a means of teaching the concept of a stack. It's true that arrays can do all that stacks can do and more, so in most real-world situations, you'd probably want to use an array, unless you specifically want to prevent the use of methods other than
.push()
andpop()
. Beyond that, this is also useful as a thought experiment of the type you might be asked in a technical interview.