What exactly are JavaScript Statements?
Tags: javascript, webdev, beginners, tutorial
Published: 2023-02-15 13:25:00

**What exactly are JavaScript Statements?**

[JavaScript](https://maxlockwood.dev/category/javascript/) statements are **commands that tell the browser what to do**. Semicolons are used to separate statements (`;`). The JavaScript statement is the JavaScript code that is translated line by line by the browser.

## What are the Four JavaScript Statements?

JavaScript statements are composed of:

- Values
- Operators
- Expressions
- Keywords and Comments.

This statement tells the browser to write `“Hello World.” inside an HTML element with id=“demo”`:

```javascript
document.getElementById(“demo”).innerHTML = “Hello World.”;
```

Most JavaScript programs contain a large number of JavaScript statements.
The statements are executed one by one in the order in which they were written.

## Semicolons ;

Semicolons are used to separate JavaScript statements.
At the end of each executable statement, place a semicolon:

```javascript
let a, b, c;  // Declare 3 variables
a = 10;        // Assign the value 10 to a
b = 5;        // Assign the value 5 to b
c = a + b;    // Assign the sum of a and b to c
// Output: c = 15
```

When separated by semicolons, multiple statements on one line are allowed:

```javascript
a = 10; b = 5; c = a + b;
```

## JavaScript White Space

Multiple spaces are ignored by JavaScript. You can make your script more readable by including white space.

The following lines are equivalent:

```javascript
let person = "Tom";
let person="Tom";
```

If you use a code formatter like [Prettier](https://prettier.io/) in your code editor this will automatically fix this formatting issue for you when you save your file.

To improve readability, spaces should be placed around operators (`= + - * /`):

```javascript
let x = y + z;
```

## JavaScript Line Length and Line Breaks

Code lines longer than 80 characters are often avoided by programmers for better readability.

If a JavaScript statement does not fit on one line, the best place to break it is after an operator:

```javascript
document.getElementById("demo").innerHTML =
"Hello World!";
```

## JavaScript Code Blocks

JavaScript statements can be grouped together in code blocks by enclosing them in curly brackets `{...}`
Code blocks are used to define statements that will be executed together.
JavaScript functions are one place where statements are grouped together in blocks:

```javascript
function greeting() {
  document.getElementById("demo1").innerHTML = "Hello Tom!";
  document.getElementById("demo2").innerHTML = "How are you?";
}
```

For code blocks in this tutorial, I like to use two spaces of indentation. In your dedicated code editor, you can change the size to whatever you want.

## JavaScript Keywords

JavaScript statements often start with a **keyword** to identify the JavaScript action to be performed.

Here is a list of some of the most common keywords:

| Keyword     | Description |
| ----------- | ----------- |
| var      | Declares a variable       |
| let  | Declares a block variable        |
| const      | Declares a block constant       |
| if   | Marks a block of statements to be executed on a condition        |
| switch      | Marks a block of statements to be executed in different cases       |
| for   | Marks a block of statements to be executed in a loop        |
| function     | Declares a function       |
| return   | Exits a function        |
| try   | Implements error handling to a block of statements        |

Note: JavaScript keywords are reserved words. Reserved words cannot be used as names for [variables](https://maxlockwood.dev/what-are-the-three-variables-in-javascript/).

## Conclusion

- Javascript statements are commands that instruct the browser on what action to take.
- Javascript statements can be used in addition to or instead of any Javascript object.
- Semicolons are used to separate Javascript statements (`;`)
- A single line can contain multiple javascript statements.
- A javascript statement can span multiple lines.

### Further reading

Want to learn more about statements? Then check out – [Statements and declarations – JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements)

### See also

[What is the Syntax of JavaScript?](https://maxlockwood.dev/what-is-the-syntax-of-javascript/)
[What are Functions in JavaScript?](https://maxlockwood.dev/what-are-functions-in-javascript/)
[What are the Three Variables in JavaScript?](https://maxlockwood.dev/what-are-the-three-variables-in-javascript/)

If you liked this article, then please share. You can also find me on [Twitter](https://twitter.com/maxlockwood88) for more updates.