JavaScript Math.min()
Syntax & Examples
Math.min() static-method
The Math.min() static method in JavaScript returns the smallest of zero or more numbers. If no arguments are given, the result is Infinity.
Syntax of Math.min()
There are 4 variations for the syntax of Math.min() static-method. They are:
Math.min()
This static-method returns +Infinity, since no arguments are given.
Returns value of type Number
.
Math.min(value1)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value1 | required | The first number to compare. |
This static-method returns the value of value1.
Returns value of type Number
.
Math.min(value1, value2)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value1 | required | The first number to compare. |
value2 | required | The second number to compare. |
This static-method returns the smallest of value1 and value2.
Returns value of type Number
.
Math.min(value1, value2, /* …, */ valueN)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value1 | required | The first number to compare. |
value2 | required | The second number to compare. |
valueN | optional | Additional numbers to compare. |
This static-method returns the smallest of value1, value2, and additional arguments valueN.
Returns value of type Number
.
✐ Examples
1 Using Math.min() with no arguments
In JavaScript, if no arguments are provided to the Math.min()
method, it returns Infinity.
For example,
- We call the
Math.min()
method with no arguments. - The result is stored in the variable
result
. - We log
result
to the console using theconsole.log()
method.
JavaScript Program
const result = Math.min();
console.log(result);
Output
Infinity
2 Using Math.min() with one argument
In JavaScript, we can use the Math.min()
method to find the smallest of one number, which is the number itself.
For example,
- We call the
Math.min()
method with one argument5
. - The result is stored in the variable
result
. - We log
result
to the console using theconsole.log()
method.
JavaScript Program
const result = Math.min(5);
console.log(result);
Output
5
3 Using Math.min() with multiple arguments
In JavaScript, we can use the Math.min()
method to find the smallest of multiple numbers.
For example,
- We call the
Math.min()
method with arguments10
,3
,15
, and7
. - The result is stored in the variable
result
. - We log
result
to the console using theconsole.log()
method.
JavaScript Program
const result = Math.min(10, 3, 15, 7);
console.log(result);
Output
3
Summary
In this JavaScript tutorial, we learned about min() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.