To check the topmost element unfortunately you must explicitly index it
var top = stack[stack.length-1];
the syntax stack[-1] (that would work in Python) doesn't work: negative indexes are valid only as parameters to slice call.
// The same as stack[stack.length-1], just slower and NOT idiomatic
var top = stack.slice(-1)[0];
To extract an element there is however pop:
// Add two top-most elements of the stack
var a = stack.pop();
var b = stack.pop();
stack.push(a + b);
Answer from 6502 on Stack Overflow Top answer 1 of 6
63
To check the topmost element unfortunately you must explicitly index it
var top = stack[stack.length-1];
the syntax stack[-1] (that would work in Python) doesn't work: negative indexes are valid only as parameters to slice call.
// The same as stack[stack.length-1], just slower and NOT idiomatic
var top = stack.slice(-1)[0];
To extract an element there is however pop:
// Add two top-most elements of the stack
var a = stack.pop();
var b = stack.pop();
stack.push(a + b);
2 of 6
13
Edited 2022:
Now you can just use stack.at(-1)
If you just need one edge of your stack (head or tail is not matter) use it reversed:
I mean :
peek() become array[0],
unshift(v) become push()
shift() become pop()
some code:
class Stack{
constructor(... args ){
this.store = [... args.reverse()];
}
peek(){
return this.store[0];
}
push(value){
return this.store.unshift(value);
}
pop(){
return this.store.shift();
}
}
const stack = new Stack(1,2,3);
stack.push(4);
console.log(stack.peek());
stack.pop();
console.log(stack.peek())
or shorter
function Stack(...rest){
var store = [... rest.reverse() ];
return {
push:(v)=> store.unshift(v) ,
pop : _ => store.shift(),
peek: _ => store[0]
}
}
var stack = Stack(1,2,3);
console.log(stack.peek());
stack.push(4);
console.log(stack.peek());
stack.pop(), stack.pop();
console.log(stack.peek());
Collectionsjs
collectionsjs.com › method › peek
peek() method - Collections for JavaScript
Array · peek() var array = [1, 2, 3]; array.peek(); poke(value) Replaces the value at the beginning of a collection, the value that would be returned by shift(). peekBack() Returns the value at the end of a collection, the value that would be returned by pop().
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
Array elements are object properties in the same way that toString is a property (to be specific, however, toString() is a method). Nevertheless, trying to access an element of an array as follows throws a syntax error because the property name is not valid: ... JavaScript syntax requires properties beginning with a digit to be accessed using bracket notation instead of dot notation.
Quora
quora.com › Why-is-the-JavaScript-peek-function-working-in-this-code-and-giving-i-peek-is-not-a-function
Why is the JavaScript peek() function working in this code and giving 'i.peek is not a function'? - Quora
Answer (1 of 5): Peek is relative to building Stacks and Queues. Since, peek() is not a built-in method in JavaScript. You would have to build it yourself, but that would infer that you are attempting to build your own stack or queue. Now, in reference to your question there is a possibility that...
TutorialsPoint
tutorialspoint.com › peeking-elements-from-a-stack-in-javascript
Peeking elements from a Stack in Javascript
In this section, we are going to add PEEK operation in this class. Peeking a Stack means getting the top value of the array.
npm
npmjs.com › package › array-peek
array-peek - npm
peek functionality for arrays, allow execution of functionality for each element of the array and continue procesing of the same. Latest version: 1.0.233, last published: a year ago.
» npm install array-peek
Published Jan 27, 2025
Version 1.0.233
Author kanekotic
Repository https://github.com/kanekotic/array-peek
Stack Overflow
stackoverflow.com › questions › 57700871 › why-does-js-array-not-include-a-peek-function
javascript - Why does JS array not include a peek() function? - Stack Overflow
JavaScript arrays include functions that make it handle like a stack out of the box. Refer to push(...) and pop(). If the spec/developers of the language went to the trouble to make arrays stack-like in this way with push/pop functions, why was peek() omitted (forcing us instead to always need to index the final element of the array, e.g.
GeeksforGeeks
geeksforgeeks.org › javascript › implementation-stack-javascript
Stack in JavaScript - GeeksforGeeks
Occurs when you try to perform a pop or peek operation on an empty stack. Handling: Check if the stack is empty before performing these operations. ... Occurs when you try to push an element into a stack that has reached its maximum capacity (in languages or implementations where the stack size is fixed). Handling: Check if the stack is full before performing a push operation. In a stack implementation, we need to do push and pop operations at the same end. In an array, we can do both operations at the end of the array (or last element) in O(1) time.
Published August 22, 2017
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › pop
Array.prototype.pop() - JavaScript | MDN
The pop() method of Array instances removes the last element from an array and returns that element. This method changes the length of the array.
Collectionsjs
collectionsjs.com › method › peek-back
peekBack() method - Collections for JavaScript
Array · peekBack() var array = [1, 2, 3]; array.peekBack(); pokeBack(value) Replaces the value at the end of a collection, the value that would be returned by pop(). peek() Returns the value at the beginning of a collection, the value that would be returned by shift().
W3Schools
w3schools.com › js › js_array_methods.asp
JavaScript Array Methods
Iterating arrays covers methods that operate on all array elements. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Medium
medium.com › @mohdtalib.dev › understanding-stacks-in-javascript-a-simple-guide-0e9de7691937
Stacks in JavaScript: A Simple Guide | by devtalib | Medium
August 4, 2024 - In JavaScript, a stack is usually made using an array. Let’s look at a simple example: Imagine you’re making a stack for numbers. Here’s how you can do it: class Stack { constructor() { this.items = []; } // Add a number to the stack push(number) { this.items.push(number); } // Take the top number off the stack pop() { if (this.items.length === 0) return "Oops, the stack is empty!"; return this.items.pop(); } // See what the top number is peek() { return this.items[this.items.length - 1]; } // Check if the stack is empty isEmpty() { return this.items.length === 0; } // Find out how many items are in the stack size() { return this.items.length; } } // Using the stack let myStack = new Stack(); myStack.push(5); myStack.push(10); console.log(myStack.peek()); // Output: 10 myStack.pop(); console.log(myStack.peek()); // Output: 5
GitHub
github.com › alvarolorentedev › array-peek
GitHub - alvarolorentedev/array-peek: extend array functionality to allow peek method
peek as forEach allows you to execute a callback on each of the elements of an array, not affecting the content of itself.
Author alvarolorentedev