Statements

A program is a sequence of statements executed one by one.

Syntaxstatement1; statement2;

A JavaScript program is a list of statements. Statements are executed in the order they are written, from top to bottom.

Semicolons

Statements are separated by semicolons ;. It is good practice to end every statement with one.

Whitespace

JavaScript ignores extra spaces and blank lines, so you can format code to make it readable.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Execute a series of DOM updates in sequence when a modal dialog opens.
  • Chain variable declarations and assignments as discrete steps in an initialisation routine.
  • Build a configuration object statement-by-statement before passing it to a third-party library.

More examples

Sequential variable statements

Runs three separate assignment statements in order to compute a final total with tax.

Example · js
let total = 0;
total = total + 50;
total = total * 1.1;
console.log(total); // 55

Derive a message from an array

Chains three statements to derive a count and build a message string from an array.

Example · js
const items = ["apple", "banana", "cherry"];
const count = items.length;
const message = "You have " + count + " items.";
console.log(message);

DOM manipulation statements

Executes four statements in sequence to select, style, and label a DOM element.

Example · js
const box = document.getElementById("box");
box.style.background = "#0070f3";
box.style.color = "#fff";
box.textContent = "Active";

Discussion

  • Be the first to comment on this lesson.
Statements — JavaScript | SoundsCode