JavaScript Statements And Expressions

Statements and Expressions

Statements :

Statements are instructions in JavaScript that have to be executed by web browser. JavaScript code is made up of sequence of statements and are executed in same order as they are written.

Variable declaration is the simplest example of JavaScript statement.

Syntax
var tempCelsius;

Other type of JavaScript statements include conditions / decision making, loops etc.

White (blank) spaces in statements are ignored.

It is optional to end each JavaScript statement with semicolon. But it is highly recommended to use it as it avoids possible misinterpretation of the end of the statements by JavaScript engine

Expression :

While writing client logic in JavaScript, we often combine variables and operators to do computations.

This is achieved by writing expressions.

Different types of expressions that we can write in JavaScript are:

JS Type of Expressions
JS Expression in Statement

Comment Statement :

At times, we may not want to execute a certain portion of our code or maybe we want to add information in our code that explains the significance of the particular line of code being written.

Well, when we want to exclude some part of our code from getting executed, comment statements help us.

JavaScript supports two kinds of comments.

JS Comments

Break Statement :

While we are iterating over the block of code getting executed within the loop, we may want to exit the loop if a certain condition is met. 'break' statement is used to terminate loop and transfer control to the first statement following the loop.

Syntax
Syntax :
	break;

Below example shows for loop with 5 iterations which increment variable “counter”.

When loop counter = 3, loop terminates.

Also, shown below is output for every iteration of the loop.

JS break example
JS break example

if statement used in the example is a conditional / decision-making statement which will be discussed later in the course.

Continue Statement :

There are times when during the iteration of the block of code within the loop, we may want to skip the block execution for a specific value and then continue executing the block for all the other values. JavaScript gives us 'continue' statement to handle this.

continue statement is used to terminate the current iteration of the loop and continue execution of the loop with the next iteration.

Syntax
Syntax :
	continue;

Below example shows for loop with 5 iterations which increment variable “counter”.

When loop counter = 3, the current iteration is skipped and moved to the next iteration.

Also, shown below is output for every iteration of the loop.

JS continue example
JS continue example 2

if statement used in the example is a conditional / decision-making statement which will be discussed later in the course.

#Statements_and_Expressions_in_javascript #Comment_in_javascript #Expression_in_javascipt #Break_Statement_in_javascript #Continue_Statement_in_javascript

(New page will open, for Comment)

Not yet commented...