GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • Google Chrome
  • Edge

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript Array some() Method - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript Array some() Method

Last Updated : 12 Jul, 2024

The some() method checks if any array elements pass a test provided as a callback function, returning true if any do and false if none do. It does not execute the function for empty elements or alter the original array.

Syntax

arr.some(callback(element,index,array),thisArg);

Parameters

  • callback: This parameter holds the function to be called for each element of the array.
    • element: The parameter holds the value of the elements being processed currently.
    • index: This parameter is optional, it holds the index of the currentValue element in the array starting from 0.
    • array: This parameter is optional, it holds the complete array on which Array.every is called.
  • thisArg: This parameter is optional, it holds the context to be passed as this is to be used while executing the callback function. If the context is passed, it will be used like this for each invocation of the callback function, otherwise undefined is used as default.

Return value

Returns true if any of the array elements pass the test, otherwise false.

Example: In this example the Function checkAvailability checks if a value exists in an array using some(). Function func tests checkAvailability to see if 2 and 87 exist in the array.

JavaScript
function checkAvailability(arr, val) {
    return arr.some(function (arrVal) {
        return val === arrVal;
    });
}
function func() {
    // Original function
    let arr = [2, 5, 8, 1, 4];

    // Checking for condition
    console.log(checkAvailability(arr, 2));
    console.log(checkAvailability(arr, 87));
}
func();

Output
true
false

Example : In this example the Function isGreaterThan5 checks if any element in an array is greater than 5. Function func uses some() method to verify if any element in array satisfies condition.

JavaScript
function isGreaterThan5(element, index, array) {
    return element > 5;
}
function func() {
    // Original array
    let array = [2, 5, 8, 1, 4];

    // Checking for condition in array
    let value = array.some(isGreaterThan5);

    console.log(value);
}
func();

Output
true

Example : In this example the Function isGreaterThan5 checks if any element in an array is greater than 5. Function func uses some() to verify if any element in array satisfies condition.

JavaScript
function isGreaterThan5(element, index, array) {
    return element > 5;
}
function func() {
    // Original array
    let array = [-2, 5, 3, 1, 4];

    // Checking for condition in the array
    let value = array.some(isGreaterThan5);

    console.log(value);
}
func();

Output
false

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Opera
  • Safari
Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.