JavaScript Statement & Expression

JavaScript Statements

JavaScript statements are instructions that tell the browser what to do.

A JavaScript program is made up of many statements. Each statement performs a specific task, such as creating a variable, changing HTML content, running a function, or checking a condition.

Example

Example

javascript

let name = "Amit";
console.log(name);

In this example, both lines are JavaScript statements.

What is a JavaScript Statement?

A statement is a single instruction in JavaScript.

Example

javascript

let message = "Hello JavaScript";

This statement creates a variable named message and stores a text value in it.

Another example:

Example

javascript

document.getElementById("demo").innerHTML = "Welcome!";

This statement changes the content of an HTML element.

JavaScript Statements are Executed in Order

JavaScript statements usually run from top to bottom.

Example

javascript

let firstName = "Amit";
let lastName = "Kumar";
let fullName = firstName + " " + lastName;

console.log(fullName);

Here, JavaScript first creates firstName, then lastName, then joins them into fullName.

Semicolons in JavaScript Statements

A semicolon ; is used to end a JavaScript statement.

Example

javascript

let age = 25;
let city = "Delhi";

JavaScript can sometimes work without semicolons, but using semicolons is a good habit for beginners.

Example

javascript

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

Each line is a separate statement.

Multiple Statements on One Line

You can write multiple statements on one line by separating them with semicolons.

Example

javascript

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

This works, but it is not easy to read.

Better way:

Example

javascript

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

Writing one statement per line makes code cleaner.

JavaScript Code Blocks

A code block is a group of statements written inside curly braces {}.

Code blocks are used with functions, conditions, loops, and other JavaScript structures.

Example

javascript

{
  let name = "Amit";
  console.log(name);
}

Here, both statements are inside one block.

Statements Inside a Function

A function can contain multiple statements.

Example

javascript

function showMessage() {
  let message = "Hello JavaScript";
  console.log(message);
}

In this example, the statements inside the function run when the function is called.

Example

javascript

showMessage();

Statements Inside a Condition

Statements can also be written inside conditions.

Example

javascript

let age = 18;

if (age >= 18) {
  console.log("You are eligible.");
}

Here, the statement inside the if block runs only when the condition is true.

Statements Inside a Loop

Loops also use statements inside code blocks.

Example

javascript

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

Here, the statement inside the loop runs multiple times.

Common Types of JavaScript Statements

JavaScript has many types of statements.

Statement TypeExampleUse
Variable statementlet name = "Amit";Creates a variable
Assignment statementname = "Rahul";Changes a value
Output statementconsole.log(name);Displays output in console
Function statementfunction test() {}Creates a function
Conditional statementif (age >= 18) {}Checks a condition
Loop statementfor (...) {}Repeats code
Return statementreturn total;Returns a value from a function

White Space in JavaScript Statements

JavaScript ignores extra spaces in most cases.

These two statements work the same:

Example

javascript

let name = "Amit";

Example

javascript

let     name     =     "Amit";

But the first one is cleaner and easier to read.

Use spaces to make your code readable, not messy.

Line Breaks in JavaScript Statements

You can break long statements into multiple lines.

Example

javascript

let message =
  "Welcome to JavaScript programming.";

This can make long code easier to read.

Another example:

Example

javascript

document.getElementById("demo").innerHTML =
  "JavaScript statements are easy to understand.";

Complete Example

Complete Example

html

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

  <h1>JavaScript Statements</h1>

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

  <script>
    let firstName = "Amit";
    let lastName = "Kumar";
    let age = 25;

    let fullName = firstName + " " + lastName;

    document.getElementById("result").innerHTML =
      "Name: " + fullName + "<br>" +
      "Age: " + age;
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try changing the values of firstName, lastName, and age.

You can also add more statements and display more information.

Important Points

  • JavaScript statements are instructions for the browser.
  • A JavaScript program is made up of statements.
  • Statements usually run from top to bottom.
  • A semicolon is used to end a statement.
  • Code blocks group multiple statements inside curly braces {}.
  • Functions, conditions, and loops can contain multiple statements.
  • Writing one statement per line makes code easier to read.

Conclusion

JavaScript statements are the basic building blocks of JavaScript programs.

Each statement performs an action, such as storing data, displaying output, checking conditions, or running functions.

Understanding statements helps you write clear, organized, and readable JavaScript code.