Sometimes we want to get a specific value from an array, and we do not want to completely loop through this array. So for example, suppose that we have an array of employees, and we have a very big list of employees. And we are only interested in one employee. In our case we are interested in cake. And we want to get this specific value or this specific employee. It would be a waste of time if we just create a loop and loop through the complete array, especially if the loop if the array is very big, for example, 100 elements or maybe, maybe more because companies nowadays have thousands of employees and even it doesn't have to be employees.
Sometimes the array itself is very big. Whatever values it has, it might have other values like ages like data, others data, and it would be a waste of time to just loop through the complete array instead of just get the value that you are interested in. So therefore, we have a very effective way in JavaScript to get only the value that we are interested in by using a keyword called break. And once we use this key word big, we will not have to look through the complete array. Once we have the value. We will get out of this over this loop and continue our code.
How are we going to implement the break? Well, it's so simple. There are three steps you need to follow in order to implement that. The first one is just create a simple loop. So you just loop through the array as we learned before. The second step is you just need to check the value that you are interested in.
In our case, we need to check the value of K. The third step is is that you need to check if it equals to your value So you need to create an if statement and check if that value equals to k. If so, you need to get out immediately. So here I have created the array employees and we have like eight values or seven values here. And I have created three, the three steps again so that we can follow along. The first step is you need to loop through the array. And as we learned before, we just create four and then let i equals to zero, and then the condition I smaller than employees, and then length, length, and then i plus plus. This way, we are gonna loop through the complete array, but obviously we don't want that.
So we need to check these the second step the second step is check the value that you are interested in what kind of value are we interested in? We are only interested in K. So we need to To check this value using the if statements, so I'm going to use if, and then employees, and then square brackets and then I, if the value that we are, we are iterating. through each time we are going to get the first each time are going to get the first element, the second iteration, we are going to get the second element and so on and so forth. If that value equals to k, k, what we need to do is, we need to implement the third step, which is if it equals to the value key, then get out. But how are we going to get out of this array?
It's so simple. You just type break. That's it. So simple just type break, and it's going to get out of the complete loop. And of course here we need to add our code that we want to implement and execute. So what we need to do here is just print out Kate, if it if the value of employees of AI equals to k, so what how are we going to do so by just using console dot log, and I'm going to pass employees and then I and that's how are we how we are gonna print Kate on the screen because if only if the value equals Kate we are going to print the value and then break out of the loop.
So let's try and save and run. And as you can see we get k, you can also add this line of code before the if statement here if I copy this line and add it before the if statement, what I'm going to what I'm going to get is that I am going to get all values until Kate and once we get to Kate, we're going to print it first here we're going to print it and then it's going to check if the value if the last value equals arcade if it does cuz then it's gonna break out. Or you can just add this line of code after the if statement here over there. If I paste it here and Save and Run, what I'm going to get is that I am going to get all values before Kate. And once it gets to Kate, it's gonna it's not going to print it out on the screen.
Because the console that log line in line here is after the break. So if you want to get all values before Kate as well as Kate, you just add the line of code here before the if statement so that you get all employees as well as Kate included, and then you break out of the loop. Or if you want to get all values all employees before Kate and you you don't want to get Kate once you get to Kate you just break just as this line of codes outside or after the if statement and if you want to get Only K, just add this line of code, we console dot log inside the if statement. So a quick recap, we just loop through the array, and then we check the value if the value equals k. We are going to print it here by using the console log printed on the screen.
And then we immediately use the break to break out of the loop. And we don't even need to move on and print or check other values because we have already found k