Home > Web Front-end > JS Tutorial > body text

How to find the maximum value of two numbers in javascript

青灯夜游
Release: 2021-11-24 16:12:08
Original
9374 people have browsed it

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);
Copy after login

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);
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template