loading

JS Statements

Example

				
					let x, y, z;    // Statement 1
x = 5;          // Statement 2
y = 6;          // Statement 3
z = x + y;      // Statement 4
				
			

JavaScript Programs

An “executed” list of “instructions” is what makes up a computer program.

These rules of programming are referred to as statements in programming languages.

A sequence of programming statements makes up a JavaScript program.

JavaScript scripts in HTML are run by the web browser.

JavaScript Statements

Statements in JavaScript consist of:

Keywords, Comments, Expressions, Operators, and Values.

This line of code advises the browser to insert “Hello Dolly.” into an HTML element that has the id=”demo” feature:

Example

				
					document.getElementById("demo").innerHTML = "Hello Dolly.";
				
			

Numerous JavaScript statements can be found in most JavaScript projects.

The statements are performed in the same texts order, one by one.

JavaScript code is frequently used to refer to JavaScript applications and statements.

Semicolons ;

To divide JavaScript statements, use semicolons.

Every readable statement needs a semicolon at the end:

Example

				
					let a, b, c;  // Declare 3 variables
a = 5;        // Assign the value 5 to a
b = 6;        // Assign the value 6 to b
c = a + b;    // Assign the sum of a and b to c
				
			

Several statements on a single line can be used when they are divided by semicolons:

				
					a = 5; b = 6; c = a + b;
				
			

You may come across situations sans semicolons online.

Semicolons are not necessary for finishing sentences, however they are strongly advised.

JavaScript White Space

Many spaces are ignored by JavaScript. To make your writing easier to read, include white space.

The matching lines are as follows:

				
					let person = "Hege";
let person="Hege";
				
			

It’s best to leave spaces between operators ( = + -* / ):

				
					let x = y + z;
				
			

JavaScript Line Length and Line Breaks

Programmers generally like to steer clear of code lines longer than 80 characters for improved readability.

The optimal place to break a JavaScript statement if it fits on more than one line is after an operator:

Example

				
					document.getElementById("demo").innerHTML =
"Hello Dolly!";
				
			

JavaScript Code Blocks

Statements in JavaScript can be grouped together between curly brackets {…} to form code blocks.

Code blocks are used to describe statements that will be performed in unity.

JavaScript functions are one area where statements are put together in blocks:

Example

				
					function myFunction() {
  document.getElementById("demo1").innerHTML = "Hello Dolly!";
  document.getElementById("demo2").innerHTML = "How are you?";
}
				
			

For code blocks, we apply two spaces of indentation in this tutorial.

Later in this session, functions will be covered in more detail.

JavaScript 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
Share this Doc

JS Statements

Or copy link

Explore Topic