Statements

Published May 26, 2023Updated Jan 31, 2024
Contribute to Docs

In JavaScript, statements are instructions that are executed by the browser or Node.js. A statement can be a simple or complex operation that performs an action, such as assigning a value to a variable, calling a function, or controlling program flow with conditional statements.

Declaration Statements

Declaration statements are variables, functions, or classes that are introduced into a program. These statements begin with a keyword followed by its identifier or name.

Variables are containers for storing data values:

var x = 5;
var y = 6;
var X = 4;
var z = x + y;

In this example, x, y, X, and z, are declared with the var keyword.

Note: JavaScript identifiers are case-sensitive. Notice that x is not the same as X.

Codebyte Example

This example shows the output values of the declared variables:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

Expression Statements

Expression statements, such as function calls or assignments, evaluate an expression and generate a value in JavaScript. They can be assigned to a variable or used as part of a larger expression and are then discarded.

// Function call
console.log('Hello World');

The second statement below re-assigns the value 5 to the variable x, while the third statement logs the value of x to the console:

var x = 5;
x = 6;
console.log(x);

The output of the above statement is as follows:

6

Codebyte Example

This example shows the re-assignment of value to variable x and demonstrates the console.log() function call:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

Conditional Statements

Conditional statements, such as if statements or switch statements, control program flow based on a condition:

// If statement
var x = 10;
if (x > 5) {
console.log('x is greater than 5');
}

Following is the output of the above code:

x is greater than 5
// Switch statement
var x = 2;
switch (x) {
case 1:
console.log('x is 1');
break;
case 2:
console.log('x is 2');
break;
default:
console.log('x is neither 1 nor 2');
}

This example shows the outputs of the switch statement:

x is 2

Codebyte example

Try the following runnable example:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

Loop Statements

Loop statements, such as while loops or for loops, repeat a block of code while a condition is true:

// While loop
var x = 4;
while (x < 5) {
console.log(x);
x++;
}
// For loop
for (let i = 0; i < 5; i++) {
console.log(i);
}

Codebyte example

The for loop works similarly to the while loop. Conventionally let keyword is used to declare a variable named i in for loops:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

Jump Statements

Jump statements, such as break or return statements, transfer control to another part of the program.

// Break statement
for (let i = 0; i < 5; i++) {
if (i === 3) {
break;
}
console.log(i);
}
// Return statement
function add(x, y) {
return x + y;
}

Codebyte Example

The following code example demonstrates the break and return statements:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn JavaScript on Codecademy