Javascript

[Javascript] JavaScript/Object/Array.every()/Method to check if every element meets a condition

OOQ 2022. 8. 25. 08:25
728x90
SMALL

#Javascript

#JavaScript/Object/Array.every()/Method to check if every element meets a condition

 

.every()

.every() is a method that checks whether all elements in an array meet a condition. Returns true if all elements satisfy the condition, false otherwise.


example

  • Check the elements of the array in ascending order, stop checking if any element does not satisfy the condition, and return false. Returns true if all elements have been inspected and no element does not satisfy the condition.
  • An empty array with no elements will unconditionally return true since there are no elements that do not satisfy the condition.
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>JavaScript</title>
		<style>
			body {
				font-family: Consolas, sans-serif;
			}
		</style>
  </head>
  <body>
		<p><strong>var jbAry1 = [ 1, 2, 3, 4 ];</strong></p>
		<script>
			var jbAry1 = [ 1, 2, 3, 4 ];
			document.write( '<p>jbAry1.every( function( x ) { return x < 5 } ) : ' + jbAry1.every( function( x ) { return x < 5 } ) + '</p>' );
			document.write( '<p>jbAry1.every( function( x ) { return x < 4 } ) : ' + jbAry1.every( function( x ) { return x < 4 } ) + '</p>' );
		</script>
		<p><strong>var jbAry2 = [];</strong></p>
		<script>
			var jbAry2 = [];
			document.write( '<p>jbAry2.every( function( x ) { return x > 4 } ) : ' + jbAry2.every( function( x ) { return x > 4 } ) + '</p>' );
		</script>
  </body>
</html>
728x90
LIST