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

How to find the maximum value of two numbers in javascript

青灯夜游
青灯夜游Original
2021-11-24 16:12:089411browse

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)".

How to find the maximum value of two numbers in javascript

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:

How to find the maximum value of two numbers in javascript

##Method 2: Use ternary arithmetic to find

var a=4,b=3;
var max;
max=a > b ? a : b;

console.log(max);

Output:

How to find the maximum value of two numbers in javascript

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);

How to find the maximum value of two numbers in javascript

【Related recommendations:

javascript learning tutorial

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!

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