|

7 Examples of the Every Function in Underscore JS

7 Examples of the Every Function in Underscore JS

This tutorial takes a look at the _every() function in Underscore. When you need to apply a truth test to all values in an array, _every() is there to help you out. Because of JavaScript’s loose type checking, making use of _every() and the related _some() function can be helpful. _every() uses the signature of _.every(list, [predicate], [context]). The function will check each item of the given list and will return true if every item satisfies the predicate function or if list is null, undefined or empty. If the predicate function is not provided, the value of each item will be evaluated instead. Let’s take a look at seven examples of the every() function now.



_every() function example 1

The first thing we do here in this example is to set up our array of values. We have a couple of booleans, a string, a null, and an integer. If we run this code, we’ll see that not all values in the array are truthy. This is also the case during type coersion. Finally, we run all the values against a triple equal operator and we see again that all values are not true.

Click Clear


_every() function example 2

The second example has a different set of array values as we can see. There is a boolean, an integer, and a string. Running this code shows us that indeed, JavaScript considers all of these items truthy. If we use type coersion however via the double equal operator, we are returned false. This is because not every item in the list is true based on a == check. The same holds true for a === check.

Click Clear


_every() function example 3

Enough with the falsehoods we say in this third example. Here we’ll just fill up an array with 3 boolean true values. Running the code on various types of checks using both == and === produces a true result.

Click Clear


_every() function example 4

Example four of the _every() function takes a look at an integer array with 10 values in it. All are positive values. Running the code shows that all values are truthy. On a more strict comparison using ==, we see that not all values are true. If == produces this result, then === will certainly produce a similar result – and it does. Finally we check to see if all values are even via a criteria based function. We don’t even need to run the code to know what result this will produce. Of course, not all numbers are even in our array.

Click Clear


_every() function example 5

Example five is checking for mustache power. Most of our friends are on the crop dusting bandwagon as we can see in the people array of objects. Notice that we have one holdout. George would not be caught anywhere in public with any type of facial hair gracing his upper lip. In the code, we set up a hasCropDuster function to check all values in our object. When we run the code, we find it is false that all people have a mustache.

Click Clear


_every() function example 6

In this example we see we have a couple of arrays and a couple of objects. Running the code shows that even if an array or object is empty, the every function returns true. In the case of arraylist2 and object2 however, we see null values so since not all items are valid, every returns false.

Click Clear


_every() function example 7

The final example makes use of both _.some() and _.every() as part of the same check. By taking this approach, we can ensure a collection is not null, undefined, or empty and each item is also not null or undefined. Run the code to see the result.

Click Clear


7 Examples of the Every Function in Underscore JS Summary

These seven examples of the _each() function in Underscore give us a good idea of how the function works against various collections of types. You’ll find that these types of functions are good for building guard clauses and data validation in JavaScript, and in fact many popular JS libraries takes this very approach to data validation.