Statements in JavaScript

Introduction

A JavaScript program is a sequence of statements—instructions the engine executes. This chapter explains how statements differ from expressions, how blocks group code, and which statement forms you will use in almost every file. Clear statement structure makes conditionals and loops (later chapters) much easier to read.

Prerequisites

Statement vs Expression

  • Expression: evaluates to a value (2 + 2, "hi".length, user.name)
  • Statement: performs an action and does not need to be used as a value (if (...) { ... }, const x = 1;)
javascript
// Expression: produces a value
const sum = 10 + 5;
 
// Statement: declares a variable (contains an expression on the right)
const message = "ready";
 
// Expression statement: call a function for side effect
console.log(message);

Some lines are both—for example a function call is an expression used as a statement:

javascript
// Expression used as a statement (side effect only)
console.log("done");

Common Statement Types (Preview)

You will use these throughout the course:

CategoryExamples
Declarationsconst, let, var (prefer const/let)
Control flowif, for, while, switch, break, continue
Functionsfunction, return, arrow functions
Modulesimport, export (later)

This chapter focuses on structure; detailed control flow appears in dedicated loop and conditional chapters.

Declaration Statements

const and let create bindings (names) for values:

javascript
// const: binding cannot be reassigned
const appVersion = "1.0.0";
 
// let: binding can be reassigned
let attemptCount = 0;
attemptCount = 1;
 
console.log(appVersion, attemptCount);

Warning

var is legacy function-scoped syntax. Use const/let in new code unless you maintain old codebases.

Block Statements

Curly braces define a block—a compound statement containing zero or more statements:

javascript
// Block under if executes only when condition is true
const temperature = 30;
 
if (temperature > 25) {
  console.log("Warm day");
  console.log("Stay hydrated");
}

Blocks create scope for let and const (covered in depth in a later scope chapter).

javascript
// Block-scoped variable
{
  const tempId = "debug-42";
  console.log(tempId);
}
// console.log(tempId); // ReferenceError outside block

Empty Statement

A lone semicolon is an empty statement. It is legal but rarely needed:

javascript
// Empty statement (avoid in real code)
;;

Expression Statements

Any expression followed by ; can be a statement when you want its side effect:

javascript
// Increment as expression statement
let counter = 0;
counter++;
console.log(counter);

Grouping with Parentheses

Parentheses change evaluation order in expressions (not blocks):

javascript
// Multiplication before addition without parentheses
const resultA = 2 + 3 * 4;
 
// Force addition first
const resultB = (2 + 3) * 4;
 
console.log(resultA, resultB);

Statement blocks use {}, not ().

Putting Statements Together

Save as statements-demo.js:

javascript
// Simulate a tiny login attempt flow with statements only
const username = "demo";
const maxAttempts = 3;
let attempts = 0;
 
attempts = attempts + 1;
console.log(`Attempt ${attempts} for ${username}`);
 
if (attempts < maxAttempts) {
  console.log("You can try again.");
} else {
  console.log("Locked temporarily.");
}

Run with node statements-demo.js.

Statement Order and Early Exits

By default, statements run in order. Later you will use return, break, and continue to exit early from functions and loops.

javascript
// return ends the function early
function greet(name) {
  if (!name) {
    console.log("Name required");
    return;
  }
  console.log(`Hello, ${name}`);
}
 
greet("");
greet("Rin");

FAQ

Do all statements end with a semicolon?

Many teams require semicolons. ASI inserts them in some cases—still follow project lint rules.

What is the difference between a block and an object literal?

Both use {}, but context differs: blocks follow if, for, function, etc.; object literals appear where a value is expected. Syntax highlighters and practice make this clear over time.

Can I put const inside if?

Yes—block-scoped declarations inside blocks are common in modern JavaScript.

Is console.log(...) a statement?

Yes. It is an expression statement (function call expression used for side effect).

What should I learn after statements?

Comments, then Identifiers and Reserved Words, then variables and types.