In JavaScript, a statement is a single instruction or command that the browser interprets and executes. Statements tell the browser what to do, whether itโs calculating a value, displaying output, or controlling the flow of the program.
This tutorial will cover the types of JavaScript statements, how to use them, and some examples to help you understand their role in your code.
1. ๐ Expression Statements
An expression in JavaScript is any valid unit of code that resolves to a value. When you put an expression inside a statement, that statement is called an expression statement.
Example:
jsCopyEditlet total = 5 + 10; // "5 + 10" is an expression, and the whole line is a statement
Here, total = 5 + 10
is an expression statement. The expression 5 + 10
evaluates to 15
, and the result is assigned to the variable total
.
2. โณ Declaration Statements
A declaration is a statement that defines a variable or function. It’s used to create variables and functions that can be referenced later.
Example: Variable Declaration
jsCopyEditlet name = "Alice"; // Declaring a variable "name" with the value "Alice"
Here, the statement let name = "Alice"
declares a variable name
and assigns it the value "Alice"
.
Example: Function Declaration
jsCopyEditfunction greet() {
console.log("Hello, world!");
} // Declaring a function named "greet"
This statement defines a function greet()
that, when called, will display "Hello, world!"
in the console.
3. ๐ Control Flow Statements
Control flow statements allow you to make decisions in your code or repeat tasks. These include if statements, loops, and switch statements.
Example: If-Else Statement
jsCopyEditlet age = 18;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
The if
statement checks the condition age >= 18
and, if true, logs "Adult"
to the console. If the condition is false, it logs "Minor"
.
Example: For Loop
jsCopyEditfor (let i = 0; i < 5; i++) {
console.log(i); // Logs numbers 0 to 4
}
This loop repeats the block of code inside it, printing numbers from 0
to 4
.
4. ๐ Iteration Statements
JavaScript provides several iteration statements to perform tasks multiple times. These are typically used with loops like for
, while
, and do...while
.
Example: While Loop
jsCopyEditlet count = 0;
while (count < 3) {
console.log(count);
count++; // Increments count by 1 on each iteration
}
The while
loop continues running as long as the condition count < 3
is true, printing numbers 0
to 2
.
5. ๐ Jump Statements
Jump statements allow you to control the flow of your program in more flexible ways. These include break
, continue
, and return
.
Example: Break Statement
jsCopyEditfor (let i = 0; i < 10; i++) {
if (i === 5) {
break; // Exits the loop when i is 5
}
console.log(i);
}
In this example, the loop stops as soon as i
reaches 5
due to the break
statement.
Example: Return Statement
jsCopyEditfunction add(x, y) {
return x + y; // Exits the function and returns the result
}
The return
statement exits the function and returns a value.
6. ๐ฏ Empty Statement
An empty statement is a statement that does nothing. Itโs usually represented by a semicolon ;
.
jsCopyEdit; // This is a valid, but empty statement
Empty statements are rare, but they might be used in cases where you need a placeholder.
โ Summary of Common JavaScript Statements
Statement Type | Description | Example |
---|---|---|
Expression | Executes an expression and produces a value | let x = 5 + 2; |
Declaration | Declares a variable, function, or class | let name = "Alice"; |
Control Flow | Directs the flow of the program (if, loops, etc.) | if (age >= 18) { ... } |
Iteration | Loops through code repeatedly (for, while, etc.) | for (let i = 0; i < 5; i++) { ... } |
Jump | Breaks out of or skips code blocks | break; , continue; , return; |
Empty | A statement that does nothing | ; |
๐ฏ Final Tips:
- Statements are the building blocks of your code. They determine what happens when the program runs.
- Start with declarations and expression statements, then move on to control flow and iteration statements as you build more complex programs.
- Always practice using statements to better understand how control flows through your program.