Break Keyword Javascript

Break Keyword Javascript

The break keyword is a very interesting concept and a very useful keyword. As the name says, it is used to break, out of the loop.

You might have used this break keyword in the switch statement as well. The break keyword Javascript was being used there, so as to get out after the relevant case executes. Here, you can understand the break keyword, with some logic like this – when the break keyword is encountered in the loop, we just move out of the loop. That’s it. This is like, the loop is running, and the break executes, and we are out of the loop.

Break Keyword Javascript

So, as stated earlier, we are going to have a look at the break keyword Javascript, which basically is used to break out of the loop. Do not worry if you have not got the concept properly. As we have a look at the example, you would get the idea behind it.

Now, let’s have a look at the below example, which is used to demonstrate the use of the break keyword. –


In the above example, for some reason, we are breaking out of the loop, when the value of i is 5(i do not know why we are even doing this, but yeah, we understand the break keyword this way). So, in the above program, the loop runs with peace of mind, and then, when the value of i becomes 5, the condition becomes true, the break keyword executes, and we are out of the loop. Have a look at the output –


As you can see, the break statement was executed, and as soon as this happened, we are out of the loop. This break keyword is pretty interesting and useful, and you can use the break keyword as and when required. You can also practice the programs, which involve the break keyword somehow, to get familiar with the break keyword.