Home  >  Article  >  Web Front-end  >  How to find the minimum of two numbers in JavaScript

How to find the minimum of two numbers in JavaScript

青灯夜游
青灯夜游Original
2021-06-28 16:58:498268browse

In JavaScript, you can use the min() method of the Math object to find the minimum value of two numbers. This method can return the number with the minimum value among one or more numbers; the syntax format "min( x1,x2)", the parameters x1 and x2 must be numerical values.

How to find the minimum of two numbers in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, you can use the min() method of the Math object to find the minimum of two numbers.

Let’s take an example to see how the min() method finds the minimum of two numbers.

var a=Math.min(5,10);
var b=Math.min(0,150,30,20,38);
var c=Math.min(-5,10);
var d=Math.min(-5,-10);
var e=Math.min(1.5,2.5);
console.log(a);
console.log(b);
console.log(c);
console.log(d);
console.log(e);

Output:

How to find the minimum of two numbers in JavaScript

Description: Min() method of Math object:

min () method returns the number with the smallest value among the specified numbers.

Syntax:

Math.min(n1,n2,n3,...,nX)

Parameters:

  • ##Math.min(n1,n2,n3,...,nX): one or more value. Prior to ECMASCript v3, this method had only two parameters.

Return value:

  • Number: The smallest value in the parameter. If there are no parameters, Infinity is returned. If an argument is NaN, or a non-numeric value that cannot be converted to a number, NaN is returned.

[Related recommendations:

javascript learning tutorial]

The above is the detailed content of How to find the minimum of two numbers in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn