Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the maximum between two numbers in JavaScript

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report