Get the App
SLTechnology News&Howtos  ›  Development  › 

How to find the maximum between two numbers in JavaScript

Shulou Source: shulou.com Published: 2022-06-01 18:40:14 10月03日 Update

This article mainly explains "how to find the maximum between two numbers in JavaScript". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to find the maximum between two numbers in JavaScript".

JavaScript to find the maximum value of the two methods: 1, the use of Math object max () method, syntax "Math.max (number 1, number 2);"; 2, the use of ternary operator, syntax "number 1 > number 2? number 1: number 2;"

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

The method of finding the maximum of two numbers by JavaScript

1. Using the max () method of Math object

The max () method returns the number with the highest value.

Console.log (Math.max (5,10)); console.log (Math.max (10,6)); console.log (Math.max (8,1)); console.log (Math.max (2,2.5)); console.log (Math.max (10,10))

2. Using ternary operators

The ternary operator (also known as the conditional operator) consists of a question mark and a colon and has the following syntax format:

Conditional expression? Expression 1: expression 2

If the result of the conditional expression is true (true), the code in expression 1 is executed, otherwise the code in expression 2 is executed.

Example:

Function max (first,second) {return first > second? First: second;} console.log (max (2Magne3)); console.log (max (2Magne1)); console.log (max (- 2Magne3))

3. Use if statement

Function max (first, second) {if (first > second) {return first} else {return second};} console.log (max (2,3)); console.log (max (2,1)); console.log (max (- 2,3))

Thank you for your reading, the above is the content of "how to find the maximum between two numbers in JavaScript". After the study of this article, I believe you have a deeper understanding of how to find the maximum between two numbers in JavaScript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Numbers maximum two maximum expressions methods operators operations conditions syntax ternary learning code content objects highest colons that is ideas situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Redmi Shulou Tech Info Microsoft OPPO Reno