Bitcoin Script runs on a stack: items go on top and operations take them off. Watch a simple script execute step by step with an animated diagram.
Last in, first out
A stack is like a pile of plates: you can only add to or take from the top.
- Push 2.
- Push 3. The stack is [2, 3].
OP_ADDpops 3 and 2, pushes 5.- Push 5. The stack is [5, 5].
OP_EQUALpops both, pushes TRUE because they're equal.
The script ends with TRUE on top, so it succeeds.
Why a stack?
It's simple to implement correctly, uses very little memory, and every step is easy to reason about. That simplicity matters when thousands of independent nodes must agree exactly.
Limits
- At most 1,000 items on the stack at once.
- Pushed items are at most 520 bytes (outside Taproot script paths).
- Scripts and opcode counts have size limits too.
Practise on paper
Run this script yourself
Script: 3 4 OP_ADD 7 OP_EQUAL
- Push 3 → [3]
- Push 4 → [3, 4]
- OP_ADD → [7]
- Push 7 → [7, 7]
- OP_EQUAL → [TRUE]
Ends with TRUE, so it succeeds.
Two stacks, actually
Script also has an "alt stack" for temporarily setting items aside (OP_TOALTSTACK, OP_FROMALTSTACK). Standard payment scripts rarely use it.
Key takeaways
- Script uses a last-in, first-out stack.
- Data is pushed; opcodes pop, compute and push.
- A script succeeds if it ends with a true value on top.
Common questions
What counts as true?
Any non-zero value. Zero, an empty item or negative zero is false.
Can a script fail early?
Yes. Opcodes like OP_VERIFY stop execution immediately if their check fails.
Why does Bitcoin use a stack instead of variables?
A stack machine is simple to implement identically across many independent nodes, which matters for consensus.
What happens if a script fails?
The transaction is invalid and nodes reject it.