


This exception thrown by the try block will be caught by the catch block and stored inside the variable e. If it is not 4, it will print that element in the web browser’s console otherwise, it will throw an exception, saying that the number is equal to four. Inside that function, we have an if statement to check if the element at a particular position in an array is 4 or not. Inside this loop, we have passed in an anonymous function (also known as arrow function). In the try block, we use a forEach loop to iterate over this array. The main goal here is to iterate over this array using the forEach loop and then break the loop when it reaches the element 4 inside the arr array. Here, first, we have declared an array called arr, which contains elements from 1 to 5. Inside the tag, we have our actual program. This paragraph tag will be used to show the error message which will be thrown by our JavaScript program. We have one paragraph tag and the id of which is errorMsg. The below output will be printed inside the web browser console Illegal break statement Exception in JavaScript // The below output will be printed on the screen. If (element = 4) throw "number is equal to four" ĮrrorMsg = document.getElementById('errorMsg') ĮrrorMsg.innerHTML = "Error: " + e + "."
NODEJS FOREACH CODE
And the catch block will contain the code which will be used to handle the exception. The try block is where you will be writing all of the code. This is done with the help of a try.catch block.

The exception handling is nothing but handling abnormal conditions if some error occurs while running a program and avoiding unnecessary program crashes. To achieve the functionality provided by the break statement inside the array.forEach loop, we can use the concept of exception handling, which is available in JavaScript. Terminate a forEach Loop Using the try.catch Block in JavaScript So, for that, we can use exception handling. There are times where we want to terminate a forEach loop when some particular condition holds (either true or false) during the execution of the program.

The only drawback with using the forEach loop to iterate over an array is that it can’t be terminated using the break keyword. Many developers prefer using the forEach loop over the traditional for loop to iterate over an array because it is much easier to write and is much more readable.Īny loop, either a for loop or a while loop, can be terminated with the help of a break statement. The forEach loop is used to iterate over an array. JavaScript has provided us with a new way of writing the for loop, and that is the forEach loop. It can seem quite confusing, especially if you are new to programming. We have to follow these 3 elements to make a traditional for loop work. The JavaScript programming language comes with various variations of for loop, unlike other programming languages, we only get to see the traditional for loop, which consists of 3 things - initialization, condition, and increment or decrement.
