If javascript expressions are phrases that are evaluated to produce a value , statements are like sentences that are executed to make something happen. One way to make something happen is by using conditionals which control the order of execution. Many javascript statements can make something happen.The following table shows different kinds of statements.
Expression statements
The simplest kind of statements in javascript are the ones that have side effects like assignment statements which can change the value of a variable.
let x = 5 // assingning value 5 to x variable
x = x+5 // changing the value to 10
++x
multiple statements
just like an expression can contain multiple subexpressions we can include multiple statements using a statement block that is opening and closing braces. A statement block is simply a sequence of statements enclosed within curly braces. some of the examples where it can be used are the body of for loops, while loops, functions, and classes.
for(int i=0;i<5;i++){
a = i*i
b = b+1
cosole.log(a*b)
}
function square(x){
return x*x
}
javascript statements can be roughly categorized into conditionals, loops, jumps, and some miscellaneous statements. we will be looking at some of them.
Conditionals
if
if is one of the keywords. It controls the flow of execution based on the condition provided. It has the following syntax.
if(expression){ } else{ }
else if
else if is used where you need to test multiple conditions based on the outcome. It is simply a better-looking code than writing nested if conditions.
it has the following syntax.
if(expression){ } else if(expression){ } else if(expression){ }
It evaluates the expression to be true or false. If it's true the body is executed and exits without evaluating the other conditions. If the first expression evaluates to false it keeps doing it for other expressions until it reaches true value or end.
Loops
Looping statements are those that bend the path of execution back upon itself to repeat the body of the loop. Javascript has five loops which are for, while, do while, for /of, for/in
while
while is one of the keywords. It has the below syntax.
while(expression){ body }
It evaluates the expression to be true or false. If it is true the body is executed and then it jumps to the top to evaluate the expression again. It goes on till the expression evaluates to false. If the expression evaluates to false the body is skipped. The body is enclosed by curly braces if it has more than one statement otherwise they can be omitted.
for
Another fundamental javascript loop is the for loop. This is more convenient than the while loop. It has the below syntax.
for(initialize;test;increment){ body }
There are three expressions in this syntax. It initializes the variable, tests the variable and increments the variable. If the test is true the body is executed and then jumps to increment the variable. It again tests the expression. This goes on until the expression evaluates to false.
for(of)
This is new for loop which was introduced in ES6. This is completely different from the other loops. It has the below syntax.
for( let variable of iterable object){
body
}
It expects a variable before the "of" keyword and an expression that evaluates to an iterable object to its right. It only works for iterable objects like maps, arrays, and sets. Using it for a regular object throws an error. To use it on regular objects use the following.
object.keys() for properties
object.values() for values
object.entries() for properties and values
It works because these return an array of object keys or values or both which is iterable.
- for(in)
This has been part of javascript since the beginning. Its syntax is very similar to for(of) with a difference as "in" replacing the "of" keyword. Unlike for(of) this works with any object and it only loops over enumerable properties.
Jumps
return
Return is one of the keywords in javascript. It has the following syntax.
return expression
The expression can be of any type. It can be empty as well. It is used to return the value of the expression in a function. When it is used, it ends the function call immediately and the expression becomes the value of that function call. This makes any code below it not to be executed.
break
The break is one of the keywords in javascript. The keyword itself is the whole syntax. It is used to come out of the closest enclosing loop immediately without executing any code below it.
continue
It is as simple as a break. It is used to jump to the next iteration of the loop without executing the code below it. The keyword "continue" itself is the whole syntax.
Labeled statements
It has the below syntax.
identifier : statement
By labeling a statement you give it a name to refer to it elsewhere in the program. return and break are the only keywords that use label statements. One of the useful ways of using the label is when you want to come out of a statement that is not the closest enclosing loop by using break keyword.
outside: if(){
for(){
break outside
}
}
Source: