JavaScript Array some()

In this post, we will discuss some() method available in JavaScript Array.

some()

The some() method is used to check if the elements in the array are matched with the condition specified. If any of the element matched with the condition specified, true is returned, otherwise false is returned. We need to write a function for condition and pass the function name to the some() as a parameter.

Syntax:

array_name.some(function_name)

Here, 'array_name' is the name of the array, It will take 'function_name' as a parameter.

Example 1:-

Let's create an array that hold 10 subjects and check if any of the element is equal to "php" or not using some() function. So the condition we need to provide inside the function will be - subjects=="php".

CopiedCopy Code
<html>
<body>
<script>
// Create an array that hold 10 strings
const subjects1 =["php","html","node.js","java","jsp","jsp","php","php","java","php"];
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
function condition1(subjects) {
  return subjects=="php";
}
// Check if any of the element in the array is equal to "php" or not.
document.writeln("Does any of the element in the array is equal to php? <br>");
document.writeln(subjects1.some(condition1));
</script>
</body>
</html>

Output:

Subjects:
php,html,node.js,java,jsp,jsp,php,php,java,php
Does any of the element in the array is equal to php?
true

We can see that "all elements in the array are not equal to "php". So false is returned."php" exists at least once in the subjects array. so true is returned.

Example 2:-

Let's create an array that hold 5 subjects and check if any of the element is with length-3.So the condition we need to provide inside the function will be - subjects.length==3.

CopiedCopy Code
<html>
<body>
<script>
// Create an array that hold 5 strings
const subjects1 =["My-sql","html","node.js",".net","java"];
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
function condition1(subjects) {
  return subjects=="php";
}
// Check if any of the element in the array with length-3.
document.writeln("Does any of the element in the array with length-3? <br>");
document.writeln(subjects1.some(condition1));
</script>
</body>
</html>

Output:

Subjects:
My-sql,html,node.js,.net,java
Does any of the element in the array with length-3?
false

We can see that the length of all the elements in the array are not equal to 3. So false is returned.

Example 3:-

Let's create an array that hold 10 integers and check if any of the elements are greater than 40. So the condition we need to provide inside the function will be - marks>40.

CopiedCopy Code
<html>
<body>
<script>
// Create an array that hold 10 integers
const marks =[12,34,22,45,67,89,0,0,7,56];
document.writeln("Marks: <br>");
document.writeln(marks);
document.writeln("<br>");
document.writeln("<br>");
function condition1(marks) {
  return marks>40;
}
// Check if any of the elements in the array are greater than 40
document.writeln("Does any of the elements in the array greater than 40 ? <br>");
document.writeln(marks.some(condition1));
</script>
</body>
</html>

Output:

Marks:
12,34,22,45,67,89,0,0,7,56
Does any of the elements in the array greater than 40 ?
true

We can see that some elements in the array are greater than 40. So true is returned.

Example 4:-

Let's create an array that hold 10 integers and check if any of the elements are less than 10 or equal to 5. So the condition we need to provide inside the function will be - marks<10 || marks==5.

CopiedCopy Code
<html>
<body>
<script>
// Create an array that hold 10 integers
const marks =[12,34,22,45,67,89,20,20,17,56];
document.writeln("Marks: <br>");
document.writeln(marks);
document.writeln("<br>");
document.writeln("<br>");
function condition1(marks) {
  return marks<10 || marks==5;
}
// Check if any of the elements in the array is less than 10 or equal to 5
document.writeln("Does any of the elements in the array is less than 10 or equal to 5 ? <br>");
document.writeln(marks.some(condition1));
</script>
</body>
</html>

Output:

Marks:
12,34,22,45,67,89,20,20,17,56
Does any of the elements in the array is less than 10 or equal to 5 ?
false

We can see that no elements in the array is less than 10 or equal to 5. So false is returned.