# Working with flags in JavaScript

Now we will learn how to work with flags. The flag is a special variable that can only take two values: `true` and `false`. Using flags, you can solve problems that check for the absence of something: for example, you can check that there is no element with a certain value in an array. Let's get started.

Let's solve the following problem: given an array of numbers, you need to check if it has an element with the value `3` or not. If there is, we print `'+++'`, if not, we print `'---'`.

First, let's try to print `'+++'`. To do this, let's go through all the elements of our array and with if we ask if the current element is equal to the value `3`. If equal - we'll output `'+++'`:

`let arr = [1, 2, 3, 4, 5]; for (let elem of arr) { if (elem == 3) { console.log('+++'); } }`

But our solution is not very good: after all, if the array contains more than one value `3`, but several, then `'+++'` will be displayed several times. Let's remake our array (make two elements with value 3) and make sure of it:

`let arr = [1, 2, 3, 4, 3, 5]; for (let elem of arr) { if (elem == 3) { console.log('+++'); // outputs several times } }`

Let's fix the problem: end the loop with `break` if the element is already found:

`let arr = [1, 2, 3, 4, 3, 5]; for (let elem of arr) { if (elem == 3) { console.log('+++'); break; // terminates the loop } }`

Let's now try to make it so that if there are no elements with the value `3` in the array at all, `'---'` is displayed. A common mistake is to add `else` to our if - in this case, `'---'` will be displayed for all elements that are not `3`:

`let arr = [1, 2, 3, 4, 5]; for (let elem of arr) { if (elem == 3) { console.log('+++'); // outputs for element 3 } else { console.log('---'); // outputs for elements 1, 2, 4, 5 } }`

So the idea of adding `else` is a bad idea, not a working one. To solve problems of this type (such problems are quite common) they use the so-called flags.

As mentioned above, a flag is a variable that can take two values: `true` or `false`.

So, let's make the variable `flag` with the following value: if it is equal to `true`, then there is an element `3` in the array, and if `false⁅ /c⁆, then there is no such element.`

Initially, we set the variable `flag` to the value `false` - that is, we will assume that there is no element `3` in the array:

`let arr = [1, 2, 3, 4, 5]; let flag = false; // consider that element 3 is not in the array`

Then we'll run the loop with if just like we did before. If the loop detects that the array has element 3, then set the variable `flag` to `true` and exit the loop with `break`:

`let arr = [1, 2, 3, 4, 5]; let flag = false; // consider that element 3 is not in the array for (let elem of arr) { if (elem == 3) { flag = true; // the element exists - redefine the flag variable break; // get out of the loop } }`

The answer to the question, if the array has `3` or not, we can only give after the loop. And we already have this answer: after the loop, the variable `flag` could remain `false` or could change its value to `true` if the loop found `3` in the array:

`let arr = [1, 2, 3, 4, 5]; let flag = false; for (let elem of arr) { if (elem == 3) { flag = true; break; } } // here the flag variable is either true or false`

Now, after the loop, we can make an if that looks at the variable `flag` and displays `'+++'` or `'---'`:

`let arr = [1, 2, 3, 4, 5]; let flag = false; for (let elem of arr) { if (elem == 3) { flag = true; break; } } if (flag === true) { console.log('+++'); } else { console.log('---'); }`

Given an array:

`let arr = ['a', 'b', 'c', 'd', 'e'];`

Check that this array contains the element `'c'`. If yes, print `'+++'`, and if not, print `'---'`.

Write code to check if a number is prime or not. A prime number is only divisible by 1 and itself, and is not divisible by other numbers.

←→
↑
en ru