They need to match … an opening { with a closing }. Same with ( and ).
That’s it really.
You most commonly see }) when closing a callback function inside a call to another function like:
const some_array = [];
some_array.forEach(function( elem, e ){
// DO STUFF WITH ELEMENT
});
There you hare calling the forEach method on the array, which takes as an argument a function … but your defining your function right in place, so you see a function(){}
block because a standard javascript function needs the braces.
Really, it’s …
some_array.forEach( callback );
And the forEach method executes the callback for each element in the array.
You could easily define the callback first and pass it to the forEach method.
function callback( elem, e ){
// DO SOMETHING WITH ELEM
}
some_array.forEach( callback );
If that made things clearer to read.
Curly braces are used to surround blocks of code … statements in a function, a while block, an if block, statements in a for … loop, etc.
MDN Reference on code blocks.
They are also used for the creation of objects, called an object literal. So you might see …
const some_obj = {
'property_name': property_value
};
MDN Reference on Object creation.
Most code editors I know of offer opening/closing bracket highlights to help you spot the beginning and end of a bracket and that can be helpful in highlighting mismatches (where you’ve opened a block, but not closed it).
Regards,
Micheal