JavaScript Statement & Expression

JavaScript Semicolons

JavaScript semicolons are used to separate statements.

A semicolon ; tells JavaScript where one statement ends.

Example

Example

javascript

let name = "John";
let age = 25;

Here, each statement ends with a semicolon.

What is a Semicolon?

A semicolon is a punctuation mark used at the end of a JavaScript statement.

Example

javascript

let city = "London";

In this example, the semicolon appears at the end of the statement.

It tells JavaScript that this instruction is complete.

Semicolon with Variables

Semicolons are commonly used after variable declarations.

Example

javascript

let firstName = "John";
let lastName = "Smith";
let age = 30;

Each line is a separate statement, so each line ends with a semicolon.

Semicolon with Expressions

Semicolons are also used after expressions and calculations.

Example

javascript

let price = 500;
let quantity = 3;
let total = price * quantity;

Here, JavaScript calculates the total and stores it in the total variable.

Semicolon with Function Calls

A semicolon is commonly used after a function call.

Example

javascript

console.log("Hello JavaScript");

Another example:

Example

javascript

showMessage();

The semicolon ends the function call statement.

JavaScript Can Work Without Semicolons

JavaScript can often work even if you do not write semicolons.

Example

javascript

let name = "Emma"
let age = 24
console.log(name)

This code usually works because JavaScript automatically adds semicolons in many cases.

This behavior is called Automatic Semicolon Insertion.

Should You Use Semicolons?

For beginners, it is better to use semicolons.

They make code clear and help avoid unexpected errors.

Recommended style:

Example

javascript

let course = "JavaScript";
let level = "Beginner";

console.log(course);
console.log(level);

Using semicolons consistently makes your code easier to read.

Multiple Statements on One Line

If you write multiple statements on one line, semicolons are required to separate them.

Example

javascript

let a = 10; let b = 20; let total = a + b;

This works, but it is not very readable.

Better way:

Example

javascript

let a = 10;
let b = 20;
let total = a + b;

Writing one statement per line is cleaner.

Semicolon After Code Blocks

You usually do not need a semicolon after code blocks used with if, for, while, or functions.

Example

javascript

if (age >= 18) {
  console.log("Allowed");
}

No semicolon is needed after the closing curly brace.

Another example:

Example

javascript

function showMessage() {
  console.log("Hello");
}

No semicolon is required after a function declaration.

Semicolon with Arrays and Objects

When arrays or objects are assigned to variables, the statement should end with a semicolon.

Example with array:

Example

javascript

let languages = ["HTML", "CSS", "JavaScript"];

Example with object:

Example

javascript

let user = {
  name: "John",
  age: 28
};

Here, the semicolon ends the variable assignment statement.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Semicolons</title>
</head>
<body>

  <h1>JavaScript Semicolons</h1>

  <p id="result"></p>

  <script>
    let firstName = "John";
    let lastName = "Smith";
    let age = 30;

    let fullName = firstName + " " + lastName;

    document.getElementById("result").innerHTML =
      fullName + " is " + age + " years old.";
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try removing some semicolons and check whether the code still works.

Then add the semicolons again and keep the code clean.

Important Points

  • A semicolon ; is used to end a JavaScript statement.
  • JavaScript can often work without semicolons, but using them is a good habit for beginners.
  • Semicolons are useful when writing multiple statements on one line.
  • You usually do not need a semicolon after if, for, while, or function declaration blocks.
  • Use semicolons consistently to keep your code clean and readable.

Conclusion

JavaScript semicolons help separate statements clearly.

Although JavaScript can often add semicolons automatically, it is better for beginners to write them manually.

Using semicolons makes your JavaScript code more organized, readable, and less confusing.