JavaScript Statements

In HTML, JavaScript statements are "instructions" to be "executed" by the web browser.

Semicolons separate JavaScript statements.

Add a semicolon at the end of each executable statement:

Example:

let a, b, c;

EXAMPLE ❯


JavaScript Statements

JavaScript statements are composed of:

Values, Operators, Expressions, Keywords, and Comments.

This statement tells the browser to write "Hello Dolly." inside an HTML element with id="demo":

Most JavaScript programs contain many JavaScript statements.

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

EXAMPLE ❯


Semicolons ;

Semicolons separate JavaScript statements.

Add a semicolon at the end of each executable statement:

EXAMPLE ❯


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

EXAMPLE ❯


JavaScript White Space

JavaScript ignores multiple spaces. You can add white space to your script to make it more readable.

Whitespace refers to characters which are used to provide horizontal or vertical space between other characters.

Whitespace is often used to separate tokens in HTML, CSS, JavaScript, and other computer languages.

The following lines are equivalent:

Example:

let person = "Hege";

 

let person="Hege";


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:

EXAMPLE ❯


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 keywords you will learn about in this tutorial:

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