JavaScript / Object / Math.max(), Math.min()
Math.max(), Math.min()
Math.max()는 주어진 값 중에서 최댓값을, Math.min()은 최솟값을 반환합니다.
문법
Math.max( number1, number2, number3, ... ) Math.min( number1, number2, number3, ... )
예제 1
10, 20, 30 중에서 최댓값과 최솟값을 구합니다.
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> <style> body { font-family: Consolas, monospace; } </style> </head> <body> <script> document.write( '<p>Math.max( 10, 20, 30 ) : ' + Math.max( 10, 20, 30 ) + '</p>' ); document.write( '<p>Math.min( 10, 20, 30 ) : ' + Math.min( 10, 20, 30 ) + '</p>' ); </script> </body> </html>
예제 2
배열의 값 중에서 최댓값과 최솟값을 구하고 싶다면 apply 속성을 이용합니다.
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> <style> body { font-family: Consolas, monospace; } </style> </head> <body> <script> var jbNumber = [ 10, 20, 30 ] document.write( '<p>Math.max.apply( null, jbNumber ) : ' + Math.max.apply( null, jbNumber ) + '</p>' ); document.write( '<p>Math.min.apply( null, jbNumber ) : ' + Math.min.apply( null, jbNumber ) + '</p>' ); </script> </body> </html>