JavaScript Statements

Understanding JavaScript instructions and commands

⚡ What are JavaScript Statements?

JavaScript statements are instructions that tell the browser what to do. They are executed one by one in the order they appear.


// These are JavaScript statements
let x = 5;
let y = 10;
let sum = x + y;
console.log(sum);
                                    

Output:

15

Types of Statements

📝

Declaration

Create variables and functions

let name = "John";
const age = 25;
➡️

Assignment

Assign values to variables

x = 10;
name = "Jane";
🔄

Control Flow

Control program execution

if (x > 5) {
  console.log("Greater");
}
📞

Function Call

Execute functions

console.log("Hello");
alert("Welcome");

🔹 Statement Syntax

JavaScript statements are separated by semicolons and executed in sequence:

// Multiple statements
let a = 5;
let b = 10;
let c = a + b;

// Statements can be grouped in blocks
{
  let x = 1;
  let y = 2;
  console.log(x + y);
}

Output:

c = 15
3

🔹 Common JavaScript Statements

Here are some frequently used JavaScript statements:

// Variable declarations
let message = "Hello World";
const PI = 3.14159;
var count = 0;

// Conditional statements
if (count > 0) {
  console.log("Positive number");
}

// Loop statements
for (let i = 0; i < 3; i++) {
  console.log(i);
}

Output:

0
1
2

🔹 Statement vs Expression

Statements perform actions, while expressions produce values:

// Statements (perform actions)
let x = 5;              // Declaration statement
console.log("Hello");   // Function call statement

// Expressions (produce values)
5 + 3                   // Arithmetic expression
"Hello" + " World"      // String expression
x > 10                  // Boolean expression

Output:

Hello
Expressions evaluate to: 8, "Hello World", false

🧠 Test Your Knowledge

What separates JavaScript statements?