HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

JavaScript Statements

JavaScript statements are the individual instructions that tell the browser what to do. Every time your code assigns a value, calls a function, changes a web page element, or makes a decision, it does so through one or more statements.


What Is a JavaScript Statement?

A JavaScript statement is a complete command that the browser can execute. You can think of a statement as one meaningful action in your program. A script usually contains many statements, and the browser reads them in order from top to bottom.

Assign Values

Store data inside variables using statements such as let x = 5;.

Perform Actions

Run functions, update content, or calculate values step by step.

Control Flow

Use statements to make decisions and repeat tasks with conditions and loops.


A Simple Example of JavaScript Statements

In the example below, each line is a separate statement. Together, they create a variable, calculate a value, and show the result on the page.

<!DOCTYPE html>
<html lang="en">
<body>

    <p id="demo"></p>

    <script>
        let x = 5;
        let y = 6;
        let total = x + y;

        document.getElementById("demo").innerHTML = total;
    </script>

</body>
</html>
Note: JavaScript usually executes statements one after another in the exact order they are written, unless you use conditions, loops, or functions to change that flow.

Statements Are Built from Values, Operators, and Expressions

A statement often contains smaller pieces such as values, variables, operators, and expressions. The statement is the full instruction, while the expression is the part that produces a value.

let price = 10 + 5;

In this statement:

  • let price creates a variable
  • 10 + 5 is an expression
  • The full line is the complete JavaScript statement

Semicolons in JavaScript Statements

JavaScript statements are often ended with a semicolon ;. In many cases, JavaScript can insert semicolons automatically, but writing them yourself makes your code more consistent and easier to read.

let firstName = "Sara";
let lastName = "Khan";
let fullName = firstName + " " + lastName;
Tip: Ending statements with semicolons is a good habit for beginners. It reduces confusion and keeps your code style predictable.

Multiple Statements on One Line

You can place more than one statement on the same line if you separate them with semicolons. However, this is harder to read, so it is better to place each statement on its own line.

let a = 1; let b = 2; let c = a + b;
Warning: Writing many statements on one line may save space, but it makes your code less clear. Clean formatting is especially important when learning JavaScript.

JavaScript Statement Blocks

Sometimes several statements belong together. In JavaScript, curly braces { } are used to group statements into a block. Blocks are common in functions, loops, and conditional statements.

if (hour < 12) {
    greeting = "Good morning";
    message = "Have a productive day";
}

Here, both statements inside the braces run only if the condition is true.


How JavaScript Reads Statements

JavaScript ignores extra spaces and line breaks in most situations, so the following two examples work the same way:

let total = 5 + 6;
let total =
    5 + 6;

Even though both are valid, clear formatting helps other people read your code faster and helps you spot mistakes more easily.


Summary Table

Concept Meaning
Statement A complete instruction that JavaScript can execute
Expression A part of code that produces a value
Semicolon Commonly used to mark the end of a statement
Statement Block A group of statements wrapped inside { }

Key Points to Remember

  • JavaScript statements are the commands that make your program work
  • A browser normally executes statements from top to bottom
  • Statements often contain expressions, variables, values, and operators
  • Semicolons are often optional, but using them is a smart habit
  • Curly braces { } are used to group related statements into blocks
  • Writing one statement per line makes your code easier to read and maintain