Home » JavaScript

Array some() method with example in JavaScript

JavaScript some() method: Here, we are going to learn about the some() method of array in JavaScript.
Submitted by IncludeHelp, on March 02, 2019

JavaScript some() method

some() method is used to check a condition on all array elements (or specified elements) and returns true if any of the array elements matches the condition and returns false if all array elements do not match the condition.

Syntax:

    array.some(function, [value]);

Parameters: A function name and an optional value to be tested with all elements.

Ref: JS Array some() function

Return value: true or false

Example:

    Input:
    var arr1 = [10, 20, -30, -40, -50];
    var arr2 = [-10, -1, -20, -40, -50];

    //function to check elements are positive or not
    function isPositive(n){
        return n>=0;
    }
    
    Function call:
    arr1.some(isPositive);
    arr2.some(isPositive)

    Output:
    true
    false

JavaScript Code to check whether any of the array elements is positive or not using Array.every() method

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>
		//function to check positive numbers
		function isPositive(n){
			return n>=0;
		}
		
		var arr1 = [10, 20, -30, -40, -50];
		var arr2 = [-10, -1, -20, -40, -50];
		
		if(arr1.some(isPositive)==true)
			document.write("arr1 has atleast one positive value<br>");
		else
			document.write("arr1 does not have any positive value<br>");

		if(arr2.some(isPositive)==true)
			document.write("arr2 has atleast one positive value<br>");
		else
			document.write("arr2 does not have any positive value<br>");
		
	</script>
</body>
</html>

Output

arr1 has atleast one positive value
arr2 does not have any positive value

JavaScript Code to check whether any of the array elements is greater than 10 or not using Array.every() method

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>
		//function to check positive numbers
		function isPositive(n, value2test){
			return n>=value2test;
		}
		
		var arr1 = [10, 2, 20, 4, 6];
		var arr2 = [-1, 2, 3, 4, 9];
		
		if(arr1.every(isPositive, 10)==true)
			document.write("arr1 has atleast one element >=10<br>");
		else
			document.write("arr1 does not have any element >=10<br>");

		if(arr2.every(isPositive, 10)==true)
			document.write("arr2 has atleast one element >=10<br>");
		else
			document.write("arr2 does not have any element >=10<br>");
		
	</script>
</body>
</html>

Output

arr1 has atleast one element >=10
arr2 does not have any element >=10





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.