Statements
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 asX
.
Codebyte Example
This example shows the output values of the declared variables:
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 callconsole.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:
Conditional Statements
Conditional statements, such as if
statements or switch
statements, control program flow based on a condition:
// If statementvar 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 statementvar 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:
Loop Statements
Loop statements, such as while
loops or for
loops, repeat a block of code while a condition is true
:
// While loopvar x = 4;while (x < 5) {console.log(x);x++;}// For loopfor (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:
Jump Statements
Jump statements, such as break
or return
statements, transfer control to another part of the program.
// Break statementfor (let i = 0; i < 5; i++) {if (i === 3) {break;}console.log(i);}// Return statementfunction add(x, y) {return x + y;}
Codebyte Example
The following code example demonstrates the break
and return
statements:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours