Math.min()
is a built-in method in JavaScript that returns the minimum value from a list of arguments.
The Math.min()
method can be used to find the minimum value from two or more numbers.
Here’s how you can use the Math.min()
method in JavaScript:
Syntax of Math.min()
:
Math.min([value1[, value2[, ...]]])
The Math.min()
method accepts one or more arguments, which can be numbers or expressions that evaluate to numbers. The method returns the minimum value among the arguments passed.
Examples:
console.log(Math.min(1, 2, 3)); // Output: 1
console.log(Math.min(3, 2, 1)); // Output: 1
console.log(Math.min(0.1, 0.2, 0.3)); // Output: 0.1
console.log(Math.min(-1, -2, -3)); // Output: -3
console.log(Math.min(1, 2, -3)); // Output: -3
In the above examples, the Math.min()
method is used to find the minimum value among the arguments passed.
Usage:
The Math.min()
method can be used in various scenarios where you need to find the minimum value from a list of values.
Here are some examples:
1. Finding the minimum value in an array:
const numbers = [5, 3, 8, 2, 9, 1];
const minValue = Math.min(...numbers);
console.log(minValue); // Output: 1
In the above example, we have an array of numbers. We use the spread operator (...
) to pass the array elements as individual arguments to the Math.min()
method. The method returns the minimum value from the array.
2. Finding the minimum value from user input:
const num1 = parseFloat(prompt("Enter the first number: "));
const num2 = parseFloat(prompt("Enter the second number: "));
const num3 = parseFloat(prompt("Enter the third number: "));
const minValue = Math.min(num1, num2, num3);
console.log(minValue);
In the above example, we use the prompt()
method to get three numbers from the user. We convert the input to numbers using the parseFloat()
method. Finally, we use the Math.min()
method to find the minimum value among the three numbers entered by the user.
3. Finding the minimum value from an object property:
const students = [
{ name: "Alice", age: 20 },
{ name: "Bob", age: 18 },
{ name: "Charlie", age: 22 },
];
const minAge = Math.min(...students.map((student) => student.age));
console.log(minAge); // Output: 18
In the above example, we have an array of objects representing students
. Each object has a name
and age
property. We use the map()
method to extract the age
property from each object and pass it as an argument to the Math.min()
method. The method returns the minimum age from the array of students
.
Edge Cases:
1. Empty argument list:
console.log(Math.min()); // Output: Infinity
When no arguments are passed to the Math.min()
method, it returns Infinity
. This is because there is no minimum value to be found in an empty list of arguments.
2. Non-numeric arguments:
console.log(Math.min("hello", "world")); // Output: NaN
If any of the arguments passed to the Math.min()
method are non-numeric, the method returns NaN
(Not a Number). This is because NaN
is not a valid value for comparison when finding the minimum value.
3. Large values:
console.log(Math.min(1000000000000000000, 900000000000000000)); // Output: 9e+17
When dealing with very large values, the Math.min()
method may return unexpected results due to the limitations of the number representation in JavaScript.
In the above example, the method returns 9e+17
instead of the expected 900000000000000000
.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.