JavaScript statements are the individual instructions that tell the browser what to do. Every time your code assigns a value, calls a function, changes a web page element, or makes a decision, it does so through one or more statements.
A JavaScript statement is a complete command that the browser can execute. You can think of a statement as one meaningful action in your program. A script usually contains many statements, and the browser reads them in order from top to bottom.
Store data inside variables using statements such as let x = 5;.
Run functions, update content, or calculate values step by step.
Use statements to make decisions and repeat tasks with conditions and loops.
In the example below, each line is a separate statement. Together, they create a variable, calculate a value, and show the result on the page.
<!DOCTYPE html>
<html lang="en">
<body>
<p id="demo"></p>
<script>
let x = 5;
let y = 6;
let total = x + y;
document.getElementById("demo").innerHTML = total;
</script>
</body>
</html>
A statement often contains smaller pieces such as values, variables, operators, and expressions. The statement is the full instruction, while the expression is the part that produces a value.
let price = 10 + 5;
In this statement:
let price creates a variable10 + 5 is an expressionJavaScript statements are often ended with a semicolon ;. In many cases,
JavaScript can insert semicolons automatically, but writing them yourself makes your
code more consistent and easier to read.
let firstName = "Sara";
let lastName = "Khan";
let fullName = firstName + " " + lastName;
You can place more than one statement on the same line if you separate them with semicolons. However, this is harder to read, so it is better to place each statement on its own line.
let a = 1; let b = 2; let c = a + b;
Sometimes several statements belong together. In JavaScript, curly braces
{ } are used to group statements into a block. Blocks are common in
functions, loops, and conditional statements.
if (hour < 12) {
greeting = "Good morning";
message = "Have a productive day";
}
Here, both statements inside the braces run only if the condition is true.
JavaScript ignores extra spaces and line breaks in most situations, so the following two examples work the same way:
let total = 5 + 6;
let total =
5 + 6;
Even though both are valid, clear formatting helps other people read your code faster and helps you spot mistakes more easily.
| Concept | Meaning |
|---|---|
| Statement | A complete instruction that JavaScript can execute |
| Expression | A part of code that produces a value |
| Semicolon | Commonly used to mark the end of a statement |
| Statement Block | A group of statements wrapped inside { } |
{ } are used to group related statements into blocks