All examples

Valid parentheses, .

A stack decides whether every bracket closes in the right order.

valid-parentheses.js
function isValid(s) {
  const pairs = { ")": "(", "]": "[", "}": "{" };
  const stack = [];
  for (const ch of s) {
    if (ch === "(" || ch === "[" || ch === "{") {
      stack.push(ch);
    } else {
      if (stack.pop() !== pairs[ch]) return false;
    }
  }
  return stack.length === 0;
}

isValid("[({})]()");

What to watch

Watch the stack array grow on openers and shrink when a closer matches.

Reading valid parentheseson a page only gets you so far - code is a process, and the process happens at runtime where you can't normally see it. In NeonFlow, this exact program runs step by step: every call opens a frame, every branch picks a path, and every value moves through memory in front of you. That's the fastest way to actually understand how valid parentheses works.

Run valid parentheses yourself

Open NeonFlow, paste this code (or any of your own), and scrub through it one step at a time.

Open NeonFlow