TLDR Web Dev · ARTICLES & TUTORIALS
UI ALGORITHMS: A TINY UNDO STACK
Summary from the TLDR Web Dev newsletter
This article shows a simple implementation of an undo/redo stack in JavaScript. Instead of using a single stack with a pointer, the implementation uses two separate stacks, "past" and "future", to represent undoable and redoable actions, respectively, avoiding common indexing errors. It also addresses the pass-by-reference issue in JavaScript by using structuredClone() to guarantee that the undo and redo functions operate on cloned copies of the data, maintaining idempotency.