🏠 JavaScript Basics
Estimated reading: 3 minutes 10 views

📜 JavaScript Statements Explained – Types, Syntax & Best Practices

💡 What makes JavaScript run line-by-line?
That’s right — statements. Every task you ask JavaScript to perform is done through a statement.

JavaScript statements are the building blocks of code execution. Whether you’re declaring a variable, making decisions, or looping through data — you’re writing statements.


🧠 What Is a JavaScript Statement?

A JavaScript statement is an instruction that the browser executes. Think of each statement as a single step in your script.

codelet name = "Vaibhav";   // This is a statement
console.log(name); // Another statement

Each line above is a complete action — the first stores data, the second outputs it.

📝 Best Practice: Always end statements with a semicolon (;) — it’s optional but helps avoid unexpected bugs in complex code.


🧩 Types of JavaScript Statements

Here are the most commonly used types of statements in JavaScript:

Statement TypePurposeExample
Declaration StatementCreates variables or constantslet x = 10;
Expression StatementAssigns or evaluates a valuex + 5;
Conditional StatementExecutes code based on conditionif (x > 5) { ... }
Loop StatementRepeats code while condition is truefor (let i = 0; i < 5; i++)
Function StatementDeclares a functionfunction greet() { ... }
Try-Catch StatementHandles exceptions (errors)try { ... } catch (e) { ... }
Block StatementGroups multiple statements { ... }if (true) { let a = 1; }

🔍 JavaScript Statement Examples (Explained)

1️⃣ Variable Declaration Statement

codelet age = 25;
  • let is the declaration keyword
  • age is the variable name
  • = 25 assigns a value
  • ; ends the statement

✅ This line tells JavaScript to store 25 inside a variable called age.


2️⃣ Conditional Statement

codeif (age > 18) {
console.log("You are an adult");
}
  • if is the statement keyword
  • (age > 18) is the condition
  • { ... } contains a block of one or more statements

📌 The code inside the block runs only if the condition is true.


3️⃣ Loop Statement

codefor (let i = 0; i < 3; i++) {
console.log(i);
}
  • This for loop runs the block 3 times
  • Each time it prints the value of i (0, 1, 2)

🌀 Loops are made up of multiple statements that control repetition.


📦 Block Statements

Block statements group one or more individual statements.

code{
let a = 1;
let b = 2;
console.log(a + b);
}
  • { ... } creates a block
  • Variables inside are scoped to the block

📍 Used in conditionals, loops, and functions.


⚠️ JavaScript Statement vs Expression

FeatureStatementExpression
ExecutesYesYes
Returns valueNot alwaysAlways
Used as valueNoYes

🧪 Example:

codelet x = 10;        // Statement
let y = x + 5; // Expression used in a statement

🧼 Semicolon Best Practices

  • JavaScript inserts semicolons automatically, but not always correctly.
  • For reliability and clarity, end every statement with ;

✅ Safe:

codelet name = "John";
console.log(name);

🚫 Risky (especially with return or break):

codereturn
"hello" // This won’t be returned properly!

✅ Summary

🔑 Key Point💡 Description
StatementsInstructions that the JS engine executes
Always use semicolonsPrevents ambiguity and potential bugs
Types of statementsVariable, function, conditionals, loops, etc.
Group with blocks {}Useful for conditions and loops

❓FAQ: JavaScript Statements

❓ What is a JavaScript statement?

A JavaScript statement is a complete instruction, like declaring a variable or calling a function.

❓ Do I have to use semicolons in JavaScript?

While optional, semicolons are recommended to avoid automatic insertion issues.

❓ What’s the difference between a statement and an expression?

Statements perform actions. Expressions produce values and can be used inside statements.

❓ Can one line have multiple statements?

Yes, use semicolons to separate them:

codelet x = 10; let y = 20;

❓ Are blocks considered statements?

Yes, blocks group multiple statements together into a single compound statement.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

JavaScript — Statements

Or Copy Link

CONTENTS
Scroll to Top