What are the differences between JavaScript's Math.max() and Math.min() functions?
Rashid D
rashid d profile pic

JavaScript'sMath.max() andMath.min() functions are used to find the maximum and minimum values, respectively, from a set of numeric arguments. Here are the differences between these two functions: 1. Return Value: -Math.max(): Returns the largest of zero or more numbers. -Math.min(): Returns the smallest of zero or more numbers. 2. Handling Arguments: -Math.max(): Accepts any number of arguments separated by commas or an array of numbers. -Math.min(): Also accepts any number of arguments separated by commas or an array of numbers. 3. Numeric Conversion: -Math.max(): Converts non-numeric arguments to numbers before determining the maximum value. -Math.min(): Also converts non-numeric arguments to numbers before determining the minimum value. 4. Array Usage: -Math.max(): When an array is passed as an argument,Math.max() treats the array as a single argument and returns the maximum value within the array. -Math.min(): Similar toMath.max(), when an array is passed,Math.min() treats it as a single argument and returns the minimum value within the array. 5. NaN Handling: -Math.max(): If any argument isNaN,Math.max() returnsNaN. -Math.min(): Similarly, if any argument isNaN,Math.min() returnsNaN. 6. Empty Arguments: -Math.max(): When called with no arguments,Math.max() returns-Infinity. -Math.min(): Likewise, when called with no arguments,Math.min() returnsInfinity. Here's an example showcasing the differences:

1
2
3
4
5
6
7
8
9
10
11
12

console.log(Math.max(1, 2, 3)); // Output: 3
console.log(Math.min(1, 2, 3)); // Output: 1

console.log(Math.max([1, 2, 3])); // Output: NaN
console.log(Math.min([1, 2, 3])); // Output: NaN

console.log(Math.max(1, '2', 3)); // Output: 3
console.log(Math.min(1, '2', 3)); // Output: 1

console.log(Math.max()); // Output: -Infinity
console.log(Math.min()); // Output: Infinity

In summary,Math.max() returns the largest value from a set of numbers, whileMath.min() returns the smallest value. They handle arguments differently, handle arrays as a single argument, convert non-numeric arguments, and have different behavior when dealing withNaN and empty arguments.

Similar Questions

What are the differences between JavaScript's Math.floor() and Math.ceil() functions?

What are the differences between JavaScript's Math.floor() and Math.ceil()?

What are the differences between JavaScript's this and arrow functions?

What are the differences between JavaScript's setTimeout() and setImmediate() functions?

What are the differences between JavaScript's push() and concat() methods?

What are the differences between Function.bind() and arrow functions in JavaScript?

What are the differences between JavaScript's filter() and find() methods?

What are the differences between JavaScript's Array.concat() and Array.join() methods?

What are the differences between map() and forEach() in JavaScript?

What are the differences between JavaScript's Map and Set data structures?

What are the differences between JavaScript's Array.map() and Array.flatMap() methods?

What are the differences between Map and WeakMap in JavaScript?

What are the differences between JavaScript's null and undefined?

What are the differences between Date.now() and new Date() in JavaScript?

What are the differences between JavaScript's splice() and slice() methods?

What are the differences between JavaScript's splice() and slice() methods?

What are the differences between Function.call() and Function.apply() in JavaScript?

What are the differences between the map() and filter() methods in JavaScript?

What are the differences between JavaScript's Array.concat() and Array.push() methods?

What are the differences between arrow functions and regular functions in JavaScript?