JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Statements

A computer program is a list of instructions to be executed by a computer.

In a programming language, these programming instructions are called statements.

A JavaScript program is a list of programming statements.

Statements are used in JavaScript to control its program flow.

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="para":

Run code

Most JavaScript programs contain many JavaScript statements.

The statements are executed, one by one, in the same order as they are written.

Run code

Statements define what the script will do and how it will be done.


Semicolons are Optional

Simple statements in JavaScript are generally followed by a semicolon character, just as they are in C, C++, and Java.

JavaScript, however, allows you to omit this semicolon if each of your statements are placed on a separate line.

Run code

But when formatted in a single line as follows, you must use semicolons:

Run code

Ending statements with semicolon is not required, but highly recommended.

It is a good programming practice to use semicolons.



JavaScript White Space

JavaScript ignores spaces, tabs, and newlines that appear in JavaScript programs.

You can add white space to your script to make it more readable.

The following lines are equivalent:

A good practice is to put spaces around operators ( = + - * / ):


JavaScript Line Length and Line Breaks

For best readability, you should avoid code lines longer than 80 characters.

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

Run code

JavaScript Code Blocks

JavaScript statements can be grouped together in code blocks, inside curly brackets {...}.

The purpose of code blocks is to define statements to be executed together.

One place you will find statements grouped together in blocks, is in JavaScript functions.

Run code

JavaScript Keywords

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

Below is a list of some of the keywords you will learn about in this tutorial:

Keyword Description
var Declares a variable, optionally initializing it to a value
let Declares a block {} scope local variable, optionally initializing it to a value
const Declares a read-only named constant
break Terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement
continue Terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration
if...else Executes a statement if a specified condition is true. If the condition is false, another statement can be executed
switch Evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case
throw Throws a user-defined exception
try...catch Marks a block of statements to try, and specifies a response, should an exception be thrown
while Creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement
do...while Creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once
for Creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement executed in the loop
for...in Iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed
for...of Iterates over iterable objects (including arrays, array-like objects, iterators and generators), invoking a custom iteration hook with statements to be executed for the value of each distinct property