← Back to Chapters

JavaScript Statements

? JavaScript Statements

⚡ Quick Overview

JavaScript statements are the individual instructions that the browser executes, one after another. A JavaScript program (or script) is simply a collection of these statements.

  • Each statement tells the browser to do something: store a value, make a decision, call a function, and more.
  • Statements usually end with a semicolon (;) for clarity and safety.
  • Multiple statements together form the logic of your script.

? Key Concepts

  • Statement – a single instruction to be executed.
  • Semicolon – marks the end of a statement.
  • Statement block – a group of statements inside { }.
  • Keywords – reserved words like if, for, let that define actions.
  • Execution flow – the order in which statements run (top to bottom, unless controlled by conditionals/loops).

? Syntax & Theory

In JavaScript, most lines you write are statements. They typically follow this pattern:

  • Variable declarations using let, const, or var.
  • Expressions that calculate values, e.g. x + y.
  • Function calls such as console.log(sum).
  • Control-flow statements like if, for, while, which often use blocks.

A block is written using curly braces { } and can contain one or more statements. Blocks are used with functions, loops, and conditionals to group related logic.

JavaScript also has many keywords, which are special reserved words with predefined meaning (you cannot use them as variable names).

? Code Examples

? Basic Statements with Semicolons

This example shows simple variable declarations, an expression, and a function call as separate statements.

? View Code Example
let x = 5;
let y = 10;
let sum = x + y;
console.log(sum);

? Using Statement Blocks

A block groups multiple statements together inside curly braces { }. All statements in the block execute together in order.

? View Code Example
{
  let a = 10;
  let b = 20;
  console.log(a + b);
}

? Multiple Statements on One Line

You can put multiple statements on the same line separated by semicolons, but it usually hurts readability.

? View Code Example
let x = 5; let y = 6; console.log(x + y);

? Live Output & Explanation

? What the Code Prints

  • In the basic statements example, sum is 5 + 10, so the console shows: 15.
  • In the block example, a + b is 10 + 20, so the console shows: 30.
  • In the multiple statements on one line example, x + y is 5 + 6, so the console shows: 11.

All of these outputs appear in the browser’s JavaScript console, which you can open using the developer tools.

Click the button to run a JavaScript statement.

? JavaScript Keywords

Keywords are special words that tell JavaScript what kind of statement you are writing. Some commonly used ones include:

  • Variable declarations: var, let, const
  • Conditionals: if, else, switch
  • Loops: for, while, do
  • Functions: function, return

Because these words have predefined meanings, you should never use them as variable or function names.

? Tips & Best Practices

  • End statements with semicolons to avoid issues with automatic semicolon insertion and to make your code clearer.
  • Keep each statement short and focused on a single task to improve readability.
  • Use statement blocks { } to group related logic, especially in conditionals and loops.
  • Avoid putting many statements on one line; prefer one statement per line for easier debugging.
  • Always declare variables with let or const (or var in older code) to avoid creating accidental global variables.

? Try It Yourself

  • Write a script that declares two variables, adds them, and logs their sum to the console.
  • Create an if statement with a block that runs only when a condition is true (for example, when a number is greater than 10).
  • Experiment by putting multiple statements on one line using semicolons, then split them into separate lines and see which version is easier to read.
  • List five JavaScript keywords and write a small example statement for each.