๐Ÿ  JavaScript Basics
Estimated reading: 3 minutes 102 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 :
Share

JavaScript โ€” Statements

Or Copy Link

CONTENTS
Scroll to Top