27 julio, 2024

some and every methods in JavaScript | Bootcamps

Methods in JavaScript refer to those functions that belong to an object and that allow us to access its elements. The two basic reasons why we would want to access the elements of an object is because we want to analyze or manipulate them. In this post, we will teach you how to use the methods some and every in JavaScript to parse the elements of an object.

some and every methods in JavaScript

Methods some and every in JavaScript, just like methods like filter, map and forEach, are methods that allow us to analyze the elements of a array by using a function. In the case of these methods, the functions that we insert into them will be used as conditions. So what these methods will do is analyze whether the elements of the array determined whether or not they meet this condition.

While the methods filter and map they give us back a new array with filtered or modified elements, the method forEach It doesn’t give us anything back. Methods some and every They also have a different result. Instead of getting a new array, with these we will obtain a boolean response, that is, a result true or false. Below, we explain under what conditions we will obtain each result.

some

The method some JavaScript parses each element of a array from the function that we pass to it. As its name indicates, this method will return the result true when any of the elements of the array meets the inserted condition. Logically, this method will return the result false only if none of the elements of the array meets the condition.

To use the method some, we insert the same command of any method array.method. Then, between its parentheses, we insert the function that we want to use as a condition to analyze. To use a function as a parameter, we can insert your operation directly or call it by name.

Here is an example of how to use the method some. First, imagine that we have the following arraywhich lists different strings of colors.

const arrayColors = [‘rojo’, ‘azul’, ‘amarillo’, ‘aguamarina’, ‘verde’]

Now we can use the method some to find if any of the strings in this arrangement they have a length with an even number. To do this, we will use the modulo arithmetic operator. To define this condition, we will insert the function into a constant lengthPair:

const lengthPar = (element) element.length % 2 === 0

So, we can call this constant to use its function as a parameter in the method some. To get this result painted in our terminal, we use the command console.log:

console.log (arrayColors.some (lengthPar))

// result: true

Keep in mind that, If we analyze a array empty with method somewe will always obtain as a result falseas you can see below:

const empty array = [ ]

const null = (item) => item === null

console.log(emptyarray.some(null))

// result: false

As in other methods, here you can call the analyzed element in any way. Before we have used elements, now item. The important thing is that you use it as a parameter in your function.

Every

Methods some and every in JavaScript they work very similar, but have different results. The method every JavaScript parses each element of the array determined and returns us a result true only if all the elements of the array meet the condition.

To exemplify the use of this method, let’s use the same array ‘arrayColors’ and the ‘lengthPar’ function from the previous example:

const arrayColors = [‘rojo’, ‘azul’, ‘amarillo’, ‘aguamarina’, ‘verde’]

const lengthPar = (element) element.length % 2 === 0

Now, to paint the result that we will obtain through our terminal, just change the word some to everyas you will see in the following line:

console.log (arrayColors.every (evenLength))

// result: false

How you can see, although the methods some and every in JavaScript they run the same analysis, their results are different. In this case, the fact that only the string ‘green’ having an odd number is enough for the result to become false.

Keep in mind that, if we use the method every for analyze a array empty, we will always get the result true, whatever the condition we have defined. You can see it in the following code example:

const empty array = [ ]

const null = (item) => item === null

console.log(emptyarray.every(null))

// result: true

Do you want to continue learning?

Now that you know what the methods are and how they work some and every in JavaScript, We are sure that you want to continue learning about the elements and possibilities of this programming language. To do this, we recommend taking a look at our Full Stack Web Development Bootcamp program, where, in addition to learning to master JavaScript, you will train in other programming languages ​​for the web. What are you waiting for to become an expert in web development? Sign up now!

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *