How Programs Work
Learn how a program runs step by step, what execution order means, and how programs make decisions.
What you'll learn
- How a program executes line by line
- What an execution flow is and why it matters
- How branching changes the order of execution
- The role of memory while a program runs
Concept
How Programs Work
When a program runs, the computer reads your instructions in order, starting from the top and moving down. This is called sequential execution. Each line is performed completely before the next one begins.
Execution Flow
The path the computer takes through your code is called the flow of execution. By default it is a straight line from top to bottom. But real programs rarely just run straight down — they branch, loop, and call functions, which change the flow.
- Branching (
if/else) lets the program choose a different path based on a condition. - Looping (
for/while) makes the program repeat a section multiple times. - Functions let the program jump to a named block of code, run it, and return.
Memory While Running
While a program runs, it uses memory to store the data it is working with. When you write const age = 25, the program reserves a spot in memory, puts the value 25 there, and labels it age so you can refer to it later. When the program finishes, that memory is released.
Why Order Matters
Because execution is ordered, the same lines in a different order produce a different result. This is the #1 surprise for beginners. Moving one line up or down can change what your program does — or break it entirely. Learning to read code "in the computer's order" is a skill you build with practice.
Example
// Sequential execution — order matters!
const price = 10;
const quantity = 3;
const total = price * quantity; // 30
console.log("Total:", total);
// Branching changes the flow:
if (total > 25) {
console.log("You qualify for free shipping.");
} else {
console.log("Shipping costs $5.");
}
The program runs top to bottom. The if/else is a branch: only one of the two messages will print, depending on whether total exceeds 25.
Try it
- Text Counter
Watch a program process text step by step: it counts characters, words, and lines — each a sequential operation.
Common mistakes
The mistake
Assuming code runs all at once instead of line by line.
The fix
Read your code top to bottom, imagining one line executing at a time. This mental model prevents most ordering bugs.
The mistake
Using a variable before it has been given a value.
The fix
Declare and assign a variable before you use it. The program reads top to bottom, so a variable must exist by the time a line references it.
Related Resources
Related Snippets
- JavaScript Array Methods
See how flow works with chained operations on arrays.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.