The Block { } Operators
Definition
The block { } operators (curly braces) are used to group statements together in a single block.
Example
if (userLoggedIn) {
// Everything inside here is part of a block
showMessage();
loadDashboard();
}
Grouping Code (Block Statements)
The most basic use of { } is to bundle lines of code together. This is crucial for control structures like loops and conditionals because JavaScript expects only one statement after them.
Note
In JavaScript, the { } symbol serves different purposes depending on how it is used:
Bundle code inside if statements:
if (true) {
// bundled code lines
}
Bundle code inside loops:
for (let i=0; i<10; i++) {
// bundled code lines
}
Limit visability (scope):
{
// standalone code
}
Object literal syntax:
const user = { name: "Alice", age: 25 };
Object destructuring:
const { name, id } = person;
Named imports and exports:
import { useState, useEffect } from "react";
See Also:
Browser Support
{ } is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera |