JavaScript Statements
JavaScript statements are instructions that tell the computer what to do.
Think of them like steps in a recipe—each statement performs a task, and together they make your program work.
JavaScript runs statements one by one from top to bottom, unless you control the flow with conditions or loops.
Types of JavaScript Statements
Let’s look at the most important types of statements every beginner should know.
Variable Declarations
Used to create variables that store data.
let age = 20; // can change later
const name = "Alex"; // constant value
var score = 100; // older way (avoid in modern code)
Assignment Statements
Assign or update values using =
.
let count = 10;
count = 15;
console.log(count); // 15
Expression Statements
Run an expression (calculation or function call).
let sum = 5 + 3;
console.log("Total:", sum);
alert("Hello!"); // function call as a statement
Conditional Statements
Make decisions using if
, else if
, else
.
let age = 18;
if (age >= 18) {
console.log("You are an adult!");
} else {
console.log("You are a minor!");
}
Looping Statements
Repeat tasks without writing code again and again.
// For loop
for (let i = 1; i <= 3; i++) {
console.log("Count:", i);
}
// While loop
let num = 1;
while (num <= 3) {
console.log("Num:", num);
num++;
}
Function Statements
Functions group code into reusable blocks.
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Sara"); // Hello, Sara!
Control Flow Statements
Special statements to control loops and functions.
// Break & Continue
for (let i = 1; i <= 5; i++) {
if (i === 3) continue; // skip 3
if (i === 5) break; // stop loop
console.log(i);
}
// Return
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // 5