Array Every method in native javaScript

The Array every method of the Array prototype in native javaScript is a way to test if all elements in an array will meet a given condition or not. In the event that just one element in the array does not meet the condition, then the end result will be false. This method is then similar to that of the array some method that will return true if just one of the elements in the array will pass the test.

The array every method is then one of many built in array prototype methods that involve looping over the contents of an array to preform some kind of action like that of the array map method, or the array filter method when it comes to working with arrays in javaScript. Some might say that this method is a little redundant as there are methods like array for each method, or better yet a while loop, that can be used to easily accomplish the same task. However there are some things about it that help to make quicker work or creating a boolen value, and to do so in a standard way when it comes to the function that is given that will be called for each element.

1 - Some basic Array every method examples

In this section I will be going over a few quick simple examples of the array every method, just for the sake of getting the basic idea of what the array every method is all about. These will just be some simple examples where I am just calling the array every method off of an instance of an array of numbers, or something to that effect. So then these will just be the typical hello world style examples for people that are still fairly new to javaScript. However this is still not much of a getting started with javaScript type section, if you are still totally new to javaScript you might want to start out reading something like that.

1.1 - Just a test all for number type example

In this example I am using the array every method to check if a given array contains only numbers or not. This is done by making use of the typeof operator when working out the expression for the function that I pass to the array every method. In the body of this method the first argument will be a value of a current element in the array that is being tested, which is the norm for many array prototype methods such as this.

1
2
3
4
5
6
7
let arr = [1, 2, 3, 4]
let b = arr.every((el) => {
return typeof el === 'number';
});
console.log(b); // true

1.2 - Making an is all number helper

So then the array every method can be used to create methods that will preform a test on all elements in an array and return true of all the elements in that array pass the given test. So I can create a method like all numbs and have it so that I just pass an array, and the all nums helper will return true of all elements in the array are numbers.

1
2
3
4
5
6
7
8
let allNums = (arr) => {
return arr.every((el) => {
return typeof el === 'number';
});
};
console.log( allNums([1,2,3]) ); // true
console.log( allNums(['1','foo', true]) ); // false

2 - The array some method

Another method that is like the array every method is the array some method. This works just like the array every method and many other methods like it in that I can give if a function as an argument that will be called for each element in the array. However when using this array some method the result will be returned true in the every that just one element will resolve as true with the given method.

1
2
3
4
5
6
7
8
9
let onePlusNums = (arr) => {
return arr.some((el) => {
return typeof el === 'number';
});
};
console.log( onePlusNums([1,2,3]) ); // true
console.log( onePlusNums([1,'2','3']) ); // true
console.log( onePlusNums(['1','foo', true]) ); // false

3 - An Array tester method that can be used in an every or some mode

So then there is the question of why to bother with these native array prototype methods when it is not so hard to do the same when it comes to just looping over the contents of an array with a while loop. Well for one thing it can take some time to create a method that will do the same thing, or with custom functionally. For the most part it makes sense to just use what is there to work with in javaScript alone and move on rather than making my own methods, or utility module.

Still if the aim is to make my own utility module, then there is making some kind of test method that will work like the every method, or some method also maybe. So with that said I took a moment to work out something like this real fast.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
let testAll = (array, tester, every) => {
let pass = false,
i = 0,
len = array.length;
every = every === undefined ? true : every;
while (i < len) {
var el = array[i];
var result = tester(el, i, array);
// if every
if (every) {
pass = true;
if (!result) {
pass = false;
break;
}
}
// if not every (some)
if (!every) {
pass = false; // default pass to false
if (result) {
pass = true;
break;
}
}
i += 1;
}
return pass;
};
let isNum = (el, i, array) => {
return typeof el === 'number';
};
let arr1 = [1, 2, 'c'];
console.log(testAll(arr1, isNum, true)); // false
console.log(testAll(arr1, isNum, false)); // true
let arr2 = [1, 2, 3];
console.log(testAll(arr2, isNum, true)); // true
console.log(testAll(arr2, isNum, false)); // true

By default it will work just like the every method, but I can set a boolean to false and have it work like the some method. In any case I can break out of the while loop in the event that just one element is false when in every mode, of if just one element is true in some mode. There are many little changes that I could make to this method here and there, but the basic idea of what I had in mid is there. In any case I can not say that I make methods like this often in real projects, it is often best to just use the native methods I have found. Also when it comes to using a user space module on top of native javaScript there are all ready all kinds of professionally maintained utility libraries that have methods like this.

4 - Conclusion

This week I think I will write a few new posts on native javaScript features that I have not got around to writing about yet such as anything that I have not wrote about in the array prototype. There are a lot of things that I am familiar with all ready that I would just like to write about more on, but there are also new features that I have not done the best job of getting up to speed with that I would like to look into more.

There is also maybe taking a moment to edit and expand older posts too while I am at it such as my post on the array for each method, as well as the general concept of the for each method and alternatives. Many of these array prototype methods are just ways of doing things that are not so hard to do with just a while loop, but they are common tasks, and having these methods at the ready can help keep me from repeating code over and over again for these things when they come up.

There are all kinds of other methods that could be made like the every and some methods when it comes to making my own methods, or using some kind of utility method. In lodash there is a lodash every and lodash some method, alone with many other useful array and object methods for example.