# JavaScript Code Blocks Explained with Examples

**URL:** <https://forum.freecodecamp.org/t/javascript-code-blocks-explained-with-examples/15913>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [June 30, 2016, 5:09pm UTC](https://forum.freecodecamp.org/t/javascript-code-blocks-explained-with-examples/15913 "2016-06-30T17:09:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [June 30, 2016, 5:09pm UTC](https://forum.freecodecamp.org/t/javascript-code-blocks-explained-with-examples/15913/1 "2016-06-30T17:09:33Z")

</div>

## Introduction

In computer programming, a **block** or code **block** is a section of code which is grouped together. Blocks consist of one or more declarations and statements. A programming language that permits the creation of blocks, including blocks nested within other blocks, is called a block-structured programming language. JavaScript is one such programming language.

A **block** statement in JavaScript is used to group zero or more statements. The block is delimited by a pair of curly brackets.

```auto
{
  statement_1;
  statement_2;
  ...
  statement_n;
}

```

## Examples

The **block** statement is commonly used with control flow statements (e.g. `if...else` , `for` , `while` ) and functions.

```auto
while (x < 10) {
  x++;
}

function addNums(num1, num2) {
  let sum = 0;
  sum = num1 + num2;
  return sum;
}

```

#### Nested Blocks

```auto
for(var i = 0; i < 10; i++){
    for(var j = 0; j < 10; j++){
        console.log(i + ", " + j);
    }    
}

```

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [August 7, 2017, 1:46am UTC](https://forum.freecodecamp.org/t/javascript-code-blocks-explained-with-examples/15913/2 "2017-08-07T01:46:52Z")

</div>

Closed on August 7, 2017, 1:46am UTC


