b){max=a;}else{max=b;}"; 2. Use the ternary operation, the syntax "max=a > b? a : b; "; 3. Use the max() method of the Math object, with the syntax "Math.max(a,b)"."/> b){max=a;}else{max=b;}"; 2. Use the ternary operation, the syntax "max=a > b? a : b; "; 3. Use the max() method of the Math object, with the syntax "Math.max(a,b)".">
Home >Web Front-end >JS Tutorial >How to find the maximum value of two numbers in javascript
Method: 1. Use if statement, syntax "if(a>b){max=a;}else{max=b;}"; 2. Use ternary operation, syntax "max=a > ; b ? a : b;"; 3. Use the max() method of the Math object, with the syntax "Math.max(a,b)".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascript method to find the maximum value of two numbers
Method 1: Use if statement (this type is suitable for finding two numerical values Used for size)
var a=2,b=3; var max; if ( a > b ){ max=a; }else{ max=b; } console.log(max);
Output:
##Method 2: Use ternary arithmetic to find
var a=4,b=3; var max; max=a > b ? a : b; console.log(max);Output:
Method 3: Using the max() method of the Math object
max() method Returns the larger of two specified numbers.var a=4,b=6; var max; max=Math.max(a,b); console.log(max);【Related recommendations:
The above is the detailed content of How to find the maximum value of two numbers in javascript. For more information, please follow other related articles on the PHP Chinese website!