Skip to content
Advertisements

Array every()

“Every” function checks whether all the elements of the array satisfy the given condition or not that is provided by a function passed to it as the argument.

Syntax:

arr.every(callback(element[, index[, array]])[, thisArg])

Callback
This function argument itself takes three arguments:

  • array (optional) :  Calls the “every ()” function.
  • index (optional) : The order of the element in the array.
  • element (Compulsory): The current element being processed in the array.

The “every()” method controls all elements of the array. Returns “boolean” to “false” if one of them is incorrect.

callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed. “every” does not change the array. It only reaches the values ​​in it. It functions accordingly.


Examples

Example-1 (number control)

        var list = [5, 4, 2, 1, 3, 4, 5, 8, 7, , 6];
        function control(x) {
            return x < 10;
        }
        console.log(list.every(control))

output:

true


Example-2 (range control)


        function control(element, index, array) {
            return (element % 5 == 0);
        }
        console.log([3, 2, 63, 13, 6, 5, 86, 100, 55].every(control))
        console.log([15,25,35,25].every(control))

output:

false
true


Example-3 (name control)

        var variable = [{ name1: "rick", name2: "john" }, { name1: "alice", name2: "john" }];
        control1 = variable.every(x => x.name1 === "alice");
        control2 = variable.every(x => x.name2 === "john");
        console.log(control1)
        console.log(control2)

output:

false
true


Example-4 (isNumber control)

        let variable1 = [2, 3, 5, 7, 1, 4, 6];
        let variable2 = ["john", 2, 4, "alice"];
        let variable3 = [1, 2, , 3];
        let variable4 = [undefined, 2, 5, 7, 3];

        console.log(variable1.every(Number));
        console.log(variable2.every(Number));
        console.log(variable3.every(Number));
        console.log(variable4.every(Number));

output:

true
false
true
false

Not: undefined is not number


Example-5 (positive number control)

        function positive(x) {
            return x > 0;
        }

        let numbers1 = [5, 6, 7, 25, 48, 78, 32];
        let numbers2 = [-2, -77, 47, 25, -5];
        let numbers3 = [2, 3, 4, 5, 0, 2];

        console.log(numbers1.every(positive))
        console.log(numbers2.every(positive))
        console.log(numbers3.every(positive))

output:

true
false
false

Not: 0 is not positive number

Advertisements

3 Comments »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: