AppDividend
Latest Code Tutorials

JavaScript Math min() Function Example

0

If you want to find the minimum out of a set of numbers in JavaScript, the Math.min() method is used. Javascript min() method comes handy in the most basic of programs as well as in programs dealing with advanced algorithms. JavaScript Math.min() method initially compares with positive infinity, and that is why if no argument is passed, this method returns positive infinity.

JavaScript Math min()

Javascript min() is a static method of Math, and it can be used without creating an object. Javascript static function Math.min() returns the lowest-valued number passed into it, or NaN if any parameter isn’t a number and can’t be converted into one.

Syntax

Math.min(x, y, z, ...)

Parameter(s)

The numbers out of which the minimum is to be determined.

Return Value

The minimum of all the parameters passed.

See the following method.

 

JavaScript Math min()

Note

  1. If no argument is passed, this method returns positive infinity.
  2. If any of the arguments cannot be converted into a valid number, this method returns NaN.
  3. If the parameter is null, this method treats it as 0.
  4. If the parameter is an empty string, this method treats it as 0.
  5. If the parameter is an empty array, this method treats it as 0.
  6. If one argument is positive zero, and the other argument is negative zero, then min() method returns negative zero as it considers negative zero to be strictly smaller than positive zero.

Compatibility(Version and above)

  • Google Chrome v1
  • Internet Explorer v3
  • Firefox v1
  • Edge v12
  • Opera v3
  • Safari v1
  • Android webview v1 
  • Chrome for Android v18
  • Firefox for Android v4
  • Opera for Android v10.1
  • Safari on iOS v1
  • Samsung Internet v1.0
  • Node.js

JavaScript version: ECMAScript 1

Consider the following examples.

Javascript min() function example

The following example demonstrates the use of the min() method.

// app.js

console.log(Math.min(1, 2, 3));
console.log(Math.min(-1, 2, 3, -6));
console.log(Math.min(0, 0, -3));
console.log(Math.min(-1, -2));
console.log(Math.min(-5, -2, -3, 1));

Output

node app
1
-6
-3
-2
-5

Example 2

The following example demonstrates the case no argument is passed.

See the following code.

// app.js

console.log(Math.min());

Output

node app
Infinity

Example 3

The following code demonstrates the cases where NaN is returned and cases where it can be avoided.

// app.js

var a = "JavaScript"; 	                //non-numeric string
var b = [1, 2, 3, 4]; 		             //array with more than one element
var c;					//undefined variable
var d = {};				//empty object

console.log(Math.min(a, 1));
console.log(Math.min(b, 2));
console.log(Math.min(c, 3));
console.log(Math.min(d, 4));

var e = "23";  			           //numeric string
var f = [10]; 			         //array with a single element

console.log(Math.min(e, 5));
console.log(Math.min(f, 1));

Output

node app
NaN
NaN
NaN
NaN
5
1

Example 4

The following code demonstrates the cases where null, empty string, or an empty object is passed.

// app.js

let a = null;
let b = "";
let c = [];


console.log(Math.min(a, 1));
console.log(Math.min(b, 2));
console.log(Math.min(c, 3));

Output

node app
0
0
0

Example 5

The following example demonstrates the case where the minimum out of positive zero and negative zero is to be determined.

// app.js

let a = 0;
let b = -0;

console.log(Math.min(a, b));

Output

node app
-0

Find a minimum element of an array in Javascript

Javascript min() method is not designed to handle an array with multiple elements being passed as a parameter directly. 

The following example demonstrates an array with multiple elements being passed as a parameter directly.

// app.js

let arr = [1, 2, 3, 4];
console.log(Math.min(arr));

Output

node app
NaN

However, the min() method can be used to find the minimum of an array of elements in the following ways.

  1. Using the Array.reduce() method.
  2. Using the Function.prototype.apply() method.
  3. Using the spread operator.

Using Array.reduce() method

To find the minimum element in an array, the Array.reduce() method can be used to compare all the elements of the Array.

// app.js

let arr = [1, 2, 3, 4];
let min_element = arr.reduce(function (x, y) {
  return Math.min(x, y);
});

Consider the following example.

The following example demonstrates the use of the Array.reduce() method with min() to find the minimum element of an array.

// app.js

let arr = [34, 54, 0, -70, 64];
let min_element = arr.reduce(function (x, y) {
  return Math.min(x, y);
});

console.log(min_element);

Output

node example7
-70

Using Function.prototype.apply() method

Using Function.prototype.apply(), one can find the minimum element of an array. However, this must be used on arrays having only a few numbers of elements.

See the following code.

function findMin(arr){
   return Math.min.apply(null, arr);
}

Consider the following example.

The following example demonstrates the use of the Function.prototype.apply() method with min() to find the minimum element of an array.

// app.js

function findMin(arr) {
  return Math.min.apply(null, arr);
}

let new_arr = [45, 67, -92, 100];

console.log(findMin(new_arr));

Output

node example8
-92

Using the spread operator

The above apply() method can be implemented with the help of the spread operator (…) as well. Again, this must be used on arrays having only a few numbers of elements.

var min = Math.min(...arr); 

The following example demonstrates the use of the spread operator with min() to find the minimum element of an array.

// app.js

let arr=[10,20,-90,6];
console.log(Math.min(...arr));

Output

node example9
-90

See also

Javascript Math trunc()

Javascript Math max()

Javascript Math exp()

Javascript Math pow()

Javascript Math abs()

Javascript Math sqrt()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.