JavaScript Statement & Expression

JavaScript Code Blocks

JavaScript code blocks are used to group multiple statements together.

A code block is written inside curly braces {}.

Example

Example

javascript

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

The statements inside {} belong to the same block.

What is a Code Block?

A code block is a group of JavaScript statements.

Example

javascript

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

Here, both statements are inside one code block.

Code blocks help keep related statements together.

Code Blocks with if Statement

Code blocks are commonly used with conditions.

Example

javascript

let age = 20;

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

In this example, both console.log() statements are inside the if block.

They run only when the condition is true.

Code Blocks with Functions

Functions also use code blocks.

Example

javascript

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

The code inside {} belongs to the function.

It runs when the function is called.

Example

javascript

showMessage();

Code Blocks with Loops

Loops use code blocks to repeat multiple statements.

Example

javascript

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

The statement inside the block runs again and again while the loop condition is true.

Multiple Statements Inside a Block

A code block can contain one statement or many statements.

Example

javascript

let price = 500;
let quantity = 2;

if (quantity > 0) {
  let total = price * quantity;
  console.log("Total Price: " + total);
}

Here, the code block contains two statements.

Block Scope

Variables declared with let or const inside a block can be used only inside that block.

Example

javascript

{
  let city = "London";
  console.log(city);
}

The variable city is available inside the block.

But it cannot be used outside the block.

Example

javascript

{
  let city = "London";
}

console.log(city);

This will not work because city was created inside the block.

var Does Not Follow Block Scope

The var keyword does not follow block scope in the same way as let and const.

Example

javascript

{
  var country = "USA";
}

console.log(country);

This can work because var is not block-scoped.

In modern JavaScript, let and const are preferred because they are safer and easier to control.

Nested Code Blocks

A code block can also be written inside another code block.

Example

javascript

let isLoggedIn = true;
let isAdmin = true;

if (isLoggedIn) {
  console.log("User is logged in.");

  if (isAdmin) {
    console.log("User is an admin.");
  }
}

This is called a nested block.

Nested blocks are useful when one condition depends on another condition.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Code Blocks</title>
</head>
<body>

  <h1>JavaScript Code Blocks</h1>

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

  <script>
    let userName = "John";
    let age = 22;
    let message = "";

    if (age >= 18) {
      message = userName + " is eligible.";
    }

    document.getElementById("result").innerHTML = message;
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try changing the value of age.

Also try adding more statements inside the if block.

Important Points

  • A code block is written inside curly braces {}.
  • Code blocks group multiple statements together.
  • Code blocks are used with conditions, functions, loops, and other JavaScript structures.
  • Variables declared with let and const inside a block are block-scoped.
  • Variables declared with var do not follow block scope in the same way.
  • Nested code blocks are blocks inside other blocks.

Conclusion

JavaScript code blocks are used to group related statements together.

They are commonly used with conditions, functions, and loops.

Understanding code blocks is important because they help you write organized JavaScript code and understand how scope works.