JavaScript Statements
You already moved results to the page, a dialog, or the Console. This session zooms in on the units of work inside a script: statements. Each statement is one complete instruction. Several statements run from top to bottom unless you later learn control-flow tools that change the path.
What is a JavaScript statement and how do you write several of them?
A statement is one complete instruction the browser executes — for example logging a value or updating an element. In this course, end each statement with a semicolon (;). Write statements in the order you want them to run. Spaces and line breaks are for humans; they do not change meaning if the statements stay unambiguous. When several statements belong together as a group, wrap them in curly braces { } — that group is a code block.
Statement rules at a glance
📊 How beginners should think about statements
| Idea | What it means for you |
|---|---|
| One statement | One complete instruction (then ; in this course) |
| Semicolon | Ends the statement — make it a habit now |
| Sequence | Top to bottom: earlier lines finish before later ones |
| Whitespace | Spaces and blank lines help reading; they are not “extra syntax” |
| Line break | You may split a long statement across lines when it stays clear |
Code block { } |
A group of statements treated as one unit (you will use this often) |
One statement
Start with a single instruction. The semicolon closes it. The Console shows the result for you — visitors still need a page update if that is the goal (previous session).
console.log("One statement");
Habits to keep
- Prefer one clear job per statement while you learn
- Put
;at the end even when the engine might guess for you - Read the line out loud: if it is not a full instruction yet, you are not done
Several statements — order matters
The engine runs statements in order. The first console.log finishes before the second starts. Swap the lines and the Console order swaps too.
console.log("First");
console.log("Second");
console.log("Third");
On the page, the same rule applies: an earlier statement that sets text runs before a later one that changes it again. Order is part of the meaning.
More than one statement on a line
You can put two short statements on one line if each has its semicolon. Readable files usually keep one statement per line. Use a shared line only when the two steps are tiny and truly belong together.
console.log("A"); console.log("B");
Whitespace and line breaks
Extra spaces and blank lines do not create new statements. Breaking a long statement across lines is fine when the pieces still form one clear instruction. Do not “fix” a bug by deleting every space — fix the logic or the missing semicolon instead.
console.log(
"Still one statement"
);
console.log("Next statement");
Code blocks — statements in braces
Curly braces { } gather statements into a block. You will meet blocks inside functions, if tests, and loops soon. For now, treat a block as “these statements travel together.”
<!DOCTYPE html>
<html>
<head>
<title>Statements — Block</title>
</head>
<body>
<h1 id="msg">Waiting…</h1>
<script>
{
document.getElementById("msg").textContent = "Step one";
document.getElementById("msg").textContent = "Step two — last wins";
}
</script>
</body>
</html>
Both statements inside the braces run in order. The heading ends on “Step two — last wins” because that assignment is last. The braces do not change the top-to-bottom rule; they only group the work.
Try it yourself
- Run the three
console.loglines; then swap the first and last and confirm the Console order changed. - Put two short logs on one line with two semicolons; then split them back to two lines for readability.
- Break a string log across two lines like the whitespace example — keep it one statement.
- Open the HTML block demo; change the second assignment string and refresh.
- Optional: remove one semicolon on purpose, note any Console complaint, then put it back.
Common mistakes
- Thinking spaces or blank lines are the program (they only shape reading)
- Assuming later lines run before earlier ones
- Skipping semicolons “because it worked once,” then hitting a confusing automatic-semicolon case later
- Mixing this session with Output: statements are instructions; Output was where results appear
- Expecting Syntax (next session) already — literals, identifiers, and case sensitivity wait there
Summary
- A statement is one complete instruction
- End statements with
;in this course - Several statements run top to bottom
- Whitespace and line breaks help humans; they do not invent new statements
{ }groups statements into a code block- Next you will look at Syntax: how values and names are written inside those statements
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top