Jumps are control structures that cause a jump to another part of the program. Break, continue, labeled, return, try/catch/finally statements are examples of jumps in javascript.
Break
The break statement is used to terminate the loop immediately when it is encountered. The break statement terminates the current loop, switch, or label statement
Syntax
break [label];
Example 1: break with for Loop
Program to print the value of i
for (let i = 1; i <= 5; i++) {
// break condition
if (i == 3) {
break;
}
console.log(i);
}
//Output :
1
2
In the above program, the for loop is used to print the value of i in each iteration. This means, that when i is equal to 3, the break statement terminates the loop. Hence, the output doesn't include values greater than or equal to 3.
Example 2: break with while Loop
let i = 0;
while (i < 5) {
i++;
console.log(i);
if (i == 3) {
break;
}
}
//Output :
1
2
3
In this example, when the current value of i is 3, the break statement terminates the loop. Therefore, you see only three numbers in the output.
Example 3: break with do while Loop
let i = 0;
do {
i++;
console.log(i);
if (i == 3) {
break;
}
} while (i < 5);
//Output :
1
2
3
Continue
The continue statement terminates execution of the statements in the current iteration of the current or labeled loop and continues execution of the loop with the next iteration.
Syntax
continue [label];
Example
var i;
for (i = 1; i < 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}
//Output :
1
2
4
In the above program, for loop is used to print the value of i in each iteration.
Notice the continued statement inside the loop.
When i is equal to 3, the continue statement skips the third iteration.
Then, I become 4, and the test condition and continue statement is evaluated again.
Hence, 4 is printed in the next iterations.
Example 2
for (var i = 1; i <= 10; i++) {
if (i % 2 === 0) {
continue;
}
console.log(i);
}
//Output :
1
3
5
7
9
In the above example, when i%2==0 then it skips that iteration and continues with the next iteration. So every time only even value is printed.
Example 3
let i = 0;
do {
i++;
if (i === 3) {
continue;
}
console.log(i);
} while (i < 5);
//Output :
1
2
4
5
In the above example, the first value of i is incremented. When the value of I becomes 3 then it skips iteration because of the continued statement. Then next iteration will continue.
Labeled Statement
JavaScript label statements are used to prefix a label to an identifier. A label can be used with a break and continue statement to control the flow more precisely. A label is simply an identifier followed by a colon (:) that is applied to a statement or a block of code.
A label can be used with a break or continue statement to control the flow of the code more precisely. The label is applied to a block of code or a statement.
Label: A unique string that is Used to define the name of the block or loop.
Syntax
Label: statement
Example 1
let str = '';
loop1:
for (let i = 0; i < 5; i++) {
if (i === 1) {
continue loop1;
}
str = str + i;
}
console.log(str);
// Output : "0234"
You can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.
Example 2
for (var i = 1; i <= 10; i++) {
if (i % 2 === 0) {
continue loop1;
}
console.log(i);
}
//Output :
1
3
5
7
9
Return
The return statement stops the execution of a function and returns a value. When a return statement is called in a function, the execution of this function is stopped. If specified, a given value is returned to the function caller. If the expression is omitted, undefined is returned instead.
We can return primitive values (such as Boolean, number, string, etc.) and Object types (such as functions, objects, arrays, etc.) by using the return statement.
Syntax
return expression;
Example 1
function square(x) {
return x * x;
}
let squared2 = square(3);
console.log(squared2);
// Output : 9
Example 2
function func(x) {
let y = x * x;
}
let res = func(2);
console.log(`result: ${res}`);
// Output : "result: undefined"
In the above program, we can see that the function has no return statement in the function with the name “func”, which is calculating the product of the square of a number. It will not return the value of the expression but will print undefined as there is no return keyword specified before the expression. Therefore the function will return “undefined” instead of returning the exact value of the square of a number.
Try catch finally
The try, catch and finally, blocks are used to handle exceptions (a type of an error).
In programming, there are two types of error.
Syntax Error: Error in the syntax. For example, if you write consol.log('your result');, the above program throws a syntax error. The spelling of console is a mistake in the above code.
Runtime Error: This type of error occurs during the execution of the program.
Syntax
try {
// try_statements
}
catch(error) {
// catch_statements
}
finally() {
// codes that gets executed anyway
}
You can also use the try...catch...finally statement to handle exceptions. The final block executes both when the code runs successfully or if an error occurs.
Example 1
const x= 100, y = 'a';
try {
console.log(x/y);
console.log(a);
}
catch(error) {
console.log('Error message: ' + error);
}
finally {
console.log('Programs runs successfully');
}
//Output :
NaN
"Error message: ReferenceError: a is not defined"
"Programs runs successfully"
In the above program, an error occurs and that error is caught by the catch block. The finally block will execute in any situation
Example 2
function welcome(){
console.log("Hello world!");
}
try{
welcome()
hello()
}
catch(e){
console.log(" " + e);
}
//Output :
"Hello world!"
" ReferenceError: hello is not defined"
In the above program try section runs successfully. Catch section caught errors and display errors.
Example 3
We can also skip catch block
function welcome(){
console.log("Hello world!");
}
function bye(){
console.log("bye");
}
try{
welcome()
}
finally{
bye()
}
//Output :
"Hello world!"
"Bye"
We can see the welcome function display messages. Finally, blocks run code if the programs run successfully or not.