JavaScript - some() method

Revision:


Category : array

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

The some() method executes the function once for each array element: if it returns "true", some() returns true and stops; if it returns "false", some() returns false and stops. The method does not execute the function for empty array elements and does not change the original array.

Syntax :

        array.some(function(value, index, arr), this)
    

Parameter values:

function: required; a function to run for each array element.

function parameters: value (required; the value of the current element), index (optional; the index of the current element), arr (optional; the array the current element belongs to).

this (optional) default undefined; a value passed to the function to be used as its "this" value.

Return value: boolean: returns true if any of the elements in the array pass the test, otherwise false.

Syntax examples:

        // Arrow function
        some((element) => { ... } )
        some((element, index) => { ... } )
        some((element, index, array) => { ... } )

        // Callback function
        some(callbackFn)
        some(callbackFn, thisArg)

        // Inline callback function
        some(function(element) { ... })
        some(function(element, index) { ... })
        some(function(element, index, array){ ... })
        some(function(element, index, array) { ... }, thisArg)
    

Examples:

            const array = [1, 2, 3, 4, 5];
            // Checks whether an element is even
            const even = (element) => element % 2 === 0;
            console.log(array.some(even));
            // Expected output: true
        

Practical examples

example: check if an element passes the test.

code:
                    <div>
                        <p class="spec" id="some"></p>
                    </div>
                    <script>
                        const ages = [3, 10, 18, 20];
                        document.getElementById("some").innerHTML = ages.some(checkAdult);
                        function checkAdult(age) {
                            return age > 18;
                        }
                    </script>
                

example: test if any array element has a higher value than the input value.

input:

Values higher:

>code:
                    <div>
                        <p class="spec">input: <input type="number" id="toCheck" value="15"></p>
                        <button onclick="someFunction()">Test</button>
                        <p class="spec">Values higher: <span id="some_2"></span></p>
                    </div>
                    <script>
                        const numbers = [4, 12, 16, 20];
                        function checkValue(x) {
                            return x > document.getElementById("toCheck").value;
                        }
                        function someFunction() {
                            document.getElementById("some_2").innerHTML = numbers.some(checkValue);
                        }
                    </script>
                

example: check if values are over 18.

>code:
                    <div>
                        <p class="spec" id="some-1"></p>
                    </div>
                    <script>
                        const ages1 = [3, 10, 18, 20];
                        document.getElementById("some-1").innerHTML = ages1.some(checkAdult);
                        function checkAdult(age) {
                            return age > 18;
                        }
                    </script>