4. Flow Control Statement / Decision Making Statement

 

 Decision Making Statement

Flow control statements in JavaScript are those statements that change the flow of execution in a program according to requirements and needs of the user.


Types of Control Statements in JavaScript

1. Conditional statement

2. Unconditional statement










1. Conditional Statements in JavaScript :  A conditional statement in JavaScript is that statement that breaks the sequential flow of a program and jumps to another part of the code based on a specific condition.

1) If statement

2) If-else statement

3) Nested If-else statement

4) Switch statement



2. Unconditional Statements : An unconditional statement in JavaScript is that statement in which the flow of execution jumps to another part of code without carrying out any conditional test. It is also called unconditional execution in JavaScript.

JavaScript supports three types of unconditional statements. They are as follows:

1) Break statement

2) Continue statement

3) Return statement



1) Break statement 
- A break statement in JavaScript that breaks out a loop or switch statement. It simply tells the browser to exit the code block and move on the next line of code (if any) after the block.
- When a break statement executes inside a loop statement, the loop immediately ends at a specified condition.

Use of Break Statement
1. We can use a break inside the body of loop to come out of it.
2. We can use it inside the switch block to come out of switch block.
3. We can also use it inside the nested blocks to go to the end of the block.


2) Continue statement
- Continue statement in JavaScript is a similar to the break statement. It breaks the current iteration of loop and transfers control to the restart of the loop with a new iteration.
- In other words, continue statement tells the browser to stop the current iteration of loop and to begin a new iteration of loop.

Use of Continue 
We can use the continue statement (whether labeled or unlabeled) within the block of a while, do/while, for, or for/in loop. Using it anywhere else, it generates a syntax error.

1. If you use the continue statement within a for loop, the control will jump back to its increment expression, execute it, and then check again to determine if another iteration should continue.
2. If used within a for/in loop, then the loop starts over with the next field and continues looping from there.
3. If used within the while or do-while loop, the specified expression at the starting of loop evaluates again. If it is true, the loop body executes beginning from the top.





3. Return statement
- In JavaScript, the return statement is used to end the execution of a function and specify the value to be returned by that function. When the return statement is encountered, the function stops executing, and control is handed back to the calling code.

Uses of Return 
1. Returning a Value: The primary use of return is to specify the value that a function should return to the calling code. This allows you to compute a result within a function and pass it back for further use.
2. Exiting Early: The return statement can also be used to exit a function early if certain conditions are met. This is useful for avoiding unnecessary computation.
3. Ending Execution: The return statement also serves to terminate the execution of a function. Once a return statement is encountered, any code following it within the function is not executed.





for...in loop and for...of loop in Javascript

In JavaScript, the for...in loop and for...of loop are used for iterating over different kinds of data structures. Here are examples of both loops:


for...in loop:

The for...in loop is used to iterate over the enumerable properties of an object. It is typically used with objects, as it iterates over the keys (property names) of an object.


// Example with an object

const car = {

  make: 'Toyota',

  model: 'Camry',

  year: 2022

};


for (let key in car) {

  console.log(key + ': ' + car[key]);

}


// Output 

make: Toyota

model: Camry

year: 2022



for...of loop:

The for...of loop is used to iterate over iterable objects, such as arrays, strings, maps, sets, etc. It provides a more concise syntax compared to the for...in loop.


// Example with an array

const colors = ['red', 'green', 'blue'];


for (let color of colors) {

  console.log(color);

}


// Output

red

green

blue



Difference between for…of and for…in loops in JavaScript

1. for…of loop was introduced in ES6, whereas for…in loop introduced in ES5.

2. The for…in loop iterate through the keys of an object. In other words, for…in loop returns a list of keys. For example:


let list = [10, 20, 30];

// for...in loop returns a list of keys.

   for(let x in list) {

     console.log(x); // output: o, 1, 2.

   }


The for…of loop iterates through the values of an iterable.  For example:


let list = [10, 20, 30];

// for...of loop returns the values.

   for(let x of list) {

      console.log(x); // output: 10, 20, 30.

   }


3. The for…of loop cannot be used to iterate over an object. Whereas, we can use for…in to loop over an iterable such as arrays and strings, but we should avoid using for…in for iterable objects.


Note: Some browser may not support for…of loop introduced in ES6. To know more, visit JavaScript for…of support.







Comments

Popular posts from this blog

1. Introduction of Javascript

2. Datatypes & Variables in Javascript

6. Object and Array in Javascript