Type of Jump Statement in ECMAScript 6


Well, In this article, we will learn what is jump statement. We will also take a look at different type of jump statements. But first, we need to understand what is jump statement is all about. In JavaScript, there are few jump statement that allows you to jump from the specific place to the new location. This jump statement causes the JavaScript to jump a new location in the source code. Just like the break statement used to jump to the end of the loop or other statements.

jump statement

Jump statements are used to jump the execution to the new location. there are few statements that allow us to jump from specific location to the new location.

Now let’s understand how jump statements work.

Break

The first jump statement is the break. So the break statement used alone. Break statement causes the switch statement or the innermost enclosing loop to be exit immediately. The syntax of a break statement is very simple. Just right the break keyword  like this

I think you already have seen the example of break statement in the switch case. But break statement is not Limited for that here is the example.

For example,

So I will just specify if condition and in the expression, I will specify if I am equal to 3 then I will break the loop. Now when you execute this statement you will get 1 and 2 number on the console. Because using break keyword we break the loop execution. So when the I value is become 3 then the if condition became true and execute the break statement. Then the break statement will terminate the loop an exit from the for a loop.

JavaScript allows us to use break keyword with label statement. We will learn what is label statement after a few minutes.  But now let’s understand what is the syntax of break level statement.

The syntax of the break label statement is as follows.

Using the label with the break keyword the break execution jump to Terminate enclosing statements That have specified label.

labeled statement

Now the statement maybe label by preceding it with an identifier and a semicolon. Using the label statement JavaScript allows you to specify a name to the specific block That you can refer it to elsewhere in the program. You can label any statement although it is very helpful when it has a body, such as loops. By giving the name you can use break statement inside the body.

So here is the syntax of labeled statements

Now let’s take an example and understand how labeled statement work.

So now when you execute this statement you will get one on the console. If you omit the break label statement then the while block execute thousands of time because we did not specify any false statement in the while block to terminate the loop. Now if you remove the break level statement and execute is code your browser will print one on the console more than a thousand times and it will be continuously printing the one value on the console.

So don’t remove the break statement otherwise you could need to close your browser

Right.

Continue

Now the jump statement is similar to the break statement but there is one difference in the continue statement. Except terminating the loop continue statement restart the loop from the next iteration. The continue statement Syntax is just like as simple as a break statement.

Can also use with the label

You can use continue and continue level syntax in the body of the loop.  if you use this Syntax elsewhere then it causes a syntax error.

Let’s take an example and understand how to continue statement work.

For example,

When you execute this statement you get 1 to 10 on the console. But it does not print 5 on the console. because we use something in the code. We use if condition with continue statement. so as you can say continue statement will skip the rest of the code and continue the while loop.

So when the continue statement executes the execution goes at the top And continue the while loop without printing the X value on the console. continue statement also used with some expression. In some cases, the loop will terminate because of a false statement. In that situation, you can use the continue statement to continue the loop.

return

So mostly return statement used in the function. As the name implies return statement used to return value to its caller. Following is the syntax of Return statement.

It is a Syntax error for it to appear anywhere else. The function that contains the return statement return the expression to its caller.

For example,

So when you execute this statement you will get six on the control. So when we call the function we return value to its caller. So using the return statement in the function with return addition of two equal values. We will learn the function and the parameter and all that in the future lectures but just for now return statement is used to return a value to its caller keep that into your account.

Right.

That’s it. I hope you understand this article. we are creating the whole serious of tutorials. So don’t forget to read more articles. we love to serve you best article all the time. Thank you for supporting us.