Working with Javascript Array every() method with examples for Beginners

Javascript array.every() method calls every item in the element and checks its value against the condition. It returns boolean value true only if all items checked against condition result valid. Even if one single item does not match the condition than the result for all the items will be false.

Syntax of array every() method

array.every(callback_function(item, index, array){
    //condition checking
}, extra_arguments );

Array every() method arguments

  • callback_function: This is a plain javascript function which is called foreach and every item in an array.
    It takes three arguments item, index, array. item is a required argument that must be passed to a callback function.

    • item: This represents the current item passed into a callback function.
    • index: This represents the current item index passed into a callback function.
    • array: This contains a complete array passed into a callback function.
  • extra_arguments: This argument contains extra or additional arguments to be passed to the callback function if necessary.
    These extra arguments can be accessed inside the function through this an object. Also to be noted that extra_arguments parameter is not mandatory.
Note: every() method does not change or modify array items.

Table of Contents

Examples of implementation of Array every() method

Program to check if array contains all numbers of even number

let random_numbers = [-5, 3, 9, 13, 11, 21];

let result = random_numbers.every(function(item, index, array){
    return (item%2==0);
});

console.log("Does array random_number has any even number : "+result);

//Console Output
Does array random_number has any even number : false

Array random_numbers contain numbers belong to positive and negative integers. We’ll be checking all number is array are even for this we use condition (item%2==0) that is if item MOD 2 is equal to zero then that number will be considered are even number. The Variable result holds a boolean value which returns true if all numbers in the array are even or else returns false.

Below is another program whose array consists of only positive and negative even numbers.

let random_numbers = [4, 6, 8, 10, -12, 14];

let result = random_numbers.every(function(item, index, array){
    return (item%2==0);
});

console.log("Does array random_number has any even number : "+result);

//Console Output
Does array random_number has any even number : true

The output of this array returns true because all the numbers in the array are even. It doesn’t matter whether they are positive even numbers or negative even numbers.

The below example is modified and we have added an additional condition which is (item%2==0 && item>0). The first checks whether a number is even or not and the second condition checks the number must be positive and greater than zero.

let random_numbers = [4, 6, 8, 10, -12, 14];

let result = random_numbers.every(function(item, index, array){
    return (item%2==0 && item>0);
});

console.log("Does array random_number has any even number : "+result);

//Console Output
Does array random_number has any even number : false

In this case, this program returns the output as false because a number -12 in an array is a negative integer and if one number doesn’t match the condition then complete output changes.

Play with every method

Below is an interactive program designed to teach you more about every() method. This program checks if students have scored min marks then the output will be “All students have scored min marks” else “No students have scored min marks”.You can also add new student information by going to the “Add Student Details” section and clicking on “Add New Student Info”.

Clicking on the button “Show Result” will display the output.

Add Student Details











Output :

Students Array

  

Source Code

<div>
    <h4>Add Student Details</h4>
    <label>Student Name</label>
    <input type="text" id="name" value="" />
    <br/>
    <label>Student Total Marks</label>
    <input type="number" id="total_marks" value="" />
    <br/>
    <button type="button" onclick="add_new_student_info()" >Add New Student Info</button>
    <br/>
    <hr/>

    <label>Enter min Marks</label>
    <input type="number" id="min_total_marks" value="" />
    <br/>
    <button type="button" onclick="show_result()" >Show Result</button>
    <br/>

    <p>Output : </p>
    <p id="output"></p>
    <br/>
    <h4>Students Array</h4>
    <pre id="output_pre" ></pre>

    <script>
        let name = document.getElementById('name');
        let total_marks = document.getElementById('total_marks');
        let output = document.getElementById('output');
        let output_pre = document.getElementById('output_pre');
        let min_total_marks = document.getElementById('min_total_marks');

        let student_marks = [
            {"name" : "Suma", "total_marks" : 152.5},
            {"name" : "Akshay", "total_marks" : 250},
            {"name" : "Kiran", "total_marks" : 98},
            {"name" : "Tushar", "total_marks" : 264},
        ];

        function refresh_json(){
            output_pre.innerText = JSON.stringify(student_marks, null, 2);
        }

        refresh_json();

        function add_new_student_info(){
            student_marks.push({"name" : name.value, "total_marks" : total_marks.value});
            refresh_json();
        }

        function show_result(){
            let result = student_marks.every(check_min_marks_secured_by_all_students,{"min_marks_scored" : min_total_marks.value});

            if(result){
                output.innerText = "All students have scored min marks";
            }else{
                output.innerText = "No students have scored min marks";
            }
        }

        function check_min_marks_secured_by_all_students(student, index, array){
            let min_marks_scored = this["min_marks_scored"];
            return (student["total_marks"]>=min_marks_scored);
        }
    </script>
</div>

Conclusion

We have reached the conclusion of our post Working with Javascript Array every() method with examples for Beginners. Comment if you have any doubts and if you like then help us grow by sharing this post.

Related Posts

Summary
Review Date
Reviewed Item
Working with Javascript Array every() method with examples for Beginners
Author Rating
51star
1star1star1star1star
Software Name
Javascript Programming
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Web Development