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.
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:
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.
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.
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.
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.
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.
if statement used in the example is a conditional / decision-making statement which will be discussed later in the course.