JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Statements


In JavaScript, statements are the building blocks of the code. They are instructions that the browser executes to perform specific actions, such as declaring variables, performing calculations, controlling program flow, or interacting with the DOM. Understanding statements is essential because they form the logic of any JavaScript program. Every JavaScript program is essentially a sequence of statements executed step by step.

In this chapter, you will learn what JavaScript statements are, their types, syntax rules, practical examples, common mistakes, best practices, and real-world applications.

What Are JavaScript Statements

A JavaScript statement is a complete instruction that performs an action. Statements can be simple, like assigning a value to a variable, or complex, like loops and conditional statements. Each statement typically ends with a semicolon (;), though JavaScript allows optional semicolons in many cases due to automatic semicolon insertion.

Example

let name = "Alice"; // Variable declaration statement
console.log(name);  // Output statement

Here, the first statement declares a variable, and the second statement logs its value to the console.

Types of JavaScript Statements

1. Declaration Statements

Declaration statements define variables or constants. JavaScript uses var, let, and const for declarations.

Example

let age = 25;       // let declaration
const country = "India"; // const declaration
var city = "Delhi"; // var declaration
  • let is block-scoped and can be reassigned.

  • const is block-scoped and cannot be reassigned.

  • var is function-scoped and can lead to unexpected behavior in modern code, so it is less recommended.

2. Expression Statements

Expression statements evaluate an expression and optionally assign a value.

Example

let total = 10 + 5; // Expression statement performing addition

Here, 10 + 5 is evaluated, and the result is assigned to the variable total.

3. Conditional Statements

Conditional statements control the flow of the program based on conditions. JavaScript uses if, else if, else, and switch for conditional logic.

Example

let score = 85;

if (score >= 90) {
    console.log("Grade A");
} else if (score >= 75) {
    console.log("Grade B");
} else {
    console.log("Grade C");
}

This evaluates the score and prints the appropriate grade. Conditional statements are fundamental for decision-making in programs.

4. Loop Statements

Loops are statements that repeatedly execute a block of code until a condition is met. Common loops include for, while, and do...while.

Example

for (let i = 1; i <= 5; i++) {
    console.log("Iteration " + i);
}

This for loop prints numbers 1 through 5. Loops help automate repetitive tasks efficiently.

5. Jump Statements

Jump statements change the normal flow of execution, including break, continue, and return.

Example

for (let i = 1; i <= 5; i++) {
    if (i === 3) continue; // Skip iteration when i = 3
    console.log(i);
}

Here, continue skips printing 3, demonstrating flow control.

6. Block Statements

Block statements group multiple statements inside curly braces {}. They are often used with loops and conditionals.

Example

if (true) {
    let x = 10;
    console.log(x);
}

Blocks help organize code logically and prevent scope-related issues.

7. Empty Statements

An empty statement (;) does nothing. It can be used to satisfy syntax requirements, such as in loops with no body.

Example

for (let i = 0; i < 5; i++);

Here, the loop executes five times but has no operations inside the body.

Syntax Rules

  • Most statements end with a semicolon (;), although JavaScript often inserts them automatically.

  • Statements can span multiple lines for readability.

  • Comments can be added using // for single-line or /* */ for multi-line.

  • Statements can be nested inside blocks to form complex logic.

Practical Example Combining Multiple Statements

let name = "Alice";
let age = 20;

if (age >= 18) {
    console.log(name + " is an adult.");
} else {
    console.log(name + " is a minor.");
}

for (let i = 1; i <= 3; i++) {
    console.log("Welcome message " + i);
}

This example uses variable declarations, a conditional statement, and a loop together to demonstrate typical program flow.

Accessibility Considerations

  • Ensure that output from JavaScript statements is accessible (e.g., dynamic content updates should be readable by screen readers)

  • Avoid excessive reliance on alert() for notifications

  • Maintain predictable program flow for assistive technologies

Common Mistakes

  • Omitting semicolons in situations where they are required

  • Misusing var instead of let or const

  • Using assignment = instead of comparison == or === in conditions

  • Forgetting to close blocks with curly braces

  • Over-nesting statements, making code unreadable

Avoiding these mistakes leads to cleaner, maintainable, and bug-free code.

Best Practices

  • Use let and const instead of var for modern, safer code

  • Keep statements simple and readable

  • Group related statements inside blocks for clarity

  • Comment code to describe the purpose of complex statements

  • Test conditional and loop statements thoroughly to avoid logic errors

  • Minimize global variables and use block-scoped statements

Following these practices ensures code quality and maintainability.

Real-World Applications

JavaScript statements are used in almost every aspect of web development:

  • Variable declarations and data storage

  • Conditional rendering of content based on user actions

  • Loops for displaying lists, tables, or dynamic menus

  • Jump statements for controlling program flow in forms or applications

  • Grouping statements in functions and blocks for modular design

Mastering statements is essential for building interactive and reliable web applications.

Summary of JS Statements

JavaScript statements are the fundamental units of code that execute instructions in a program. They include declaration, expression, conditional, loop, jump, block, and empty statements. Understanding their types, syntax, and proper usage is crucial for controlling program flow, handling user input, and manipulating webpage content. Best practices, accessibility considerations, and avoiding common mistakes ensure clean, maintainable, and effective code.


Practice Questions

Q1. How do you declare a variable price with a value of 100 in JavaScript?

Q2. How can you write a JavaScript statement to assign the sum of two variables x and y to a third variable total?

Q3. How do you write a conditional statement that checks if a variable age is greater than 18 and logs "Adult" if true?

Q4. How do you write a loop that prints numbers from 1 to 5 using a for loop statement?

Q5. How do you write a function call statement that displays "Welcome!" using the alert() method?

Q6. How do you group two or more statements inside a code block using {} in JavaScript?

Q7. How do you create a constant variable named PI with the value 3.14?

Q8. What is the correct way to write multiple statements on separate lines with semicolons? Provide an example.

Q9. How do you write a statement that multiplies two numbers and stores the result in a variable named product?

Q10. How do you declare a variable status, assign it a string "active", and log it to the console?


Try a Short Quiz.

JS Statements Quiz

Finished learning? Play this simple quiz to reinforce what you just studied.


Online Code Editor and Compilor

Write and run code directly in your browser. Live preview for frontend languages.


JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

Go Back Top