Maximum of Two Numbers in JavaScript
Last Updated :
21 Feb, 2024
Finding the maximum of two numbers is a popular JavaScript operation used in many different programming tasks. This can be accomplished in a variety of ways, each having unique benefits and applications.
There are several approaches for determining the maximum of two numbers in JavaScript which are as follows:
Using Math.max()
In JavaScript, you can find the maximum of two numbers using the Math.max() method. This method takes multiple arguments and returns the largest of them. Here's how you can use it to find the maximum of two numbers:
Syntax:
Math.max(num1, num2);
Example: Finding the maximum of two numbers using Math.max() method.
JavaScript
let max = Math.max(100, 10);
console.log("The Maximum Value is ", max);
OutputThe Maximum Value is 100
Using Conditional Operator (Ternary Operator)
The conditional operator, also known as the ternary operator, is a concise way to write conditional statements in many programming languages, including JavaScript. It takes three operands: a condition followed by a question mark (?), an expression to evaluate if the condition is true, and a colon (:) followed by an expression to evaluate if the condition is false.
Syntax:
let max = num1 > num2 ? num1 : num2;
Example: Finding maximum of two numbers using the conditional operator.
JavaScript
let num1 = 80;
let num2 = 30;
let max = num1 > num2 ? num1 : num2;
console.log("The Maximum Value is ", max);
OutputThe Maximum Value is 80
Using Math.max() with Spread Syntax
This approach utilizes the Math.max() method along with the spread syntax (...) to find the maximum of two numbers. The spread syntax allows us to pass an array of numbers as arguments to Math.max(), effectively comparing all the numbers in the array and returning the maximum value.
Syntax:
Math.max(...[num1, num2]);
Example: Demonstration to find maximum of two numbers using spread syntax with Math.max() method.
JavaScript
let num1 = 150;
let num2 = 20;
let max = Math.max(...[num1, num2]);
console.log("The Maximum Value is ", max);
OutputThe Maximum Value is 150
Similar Reads
if-else to find the maximum of two numbers. In this article, we will discuss how to find the maximum of two numbers with its working example in the R Programming Language using R if-else conditions. Syntax:max_number <- if (condition) { # Code block executed if the condition is TRUE value_if_true } else { # Code block executed if the condi
2 min read
JavaScript Number MAX_SAFE_INTEGER Property The JavaScript Number.MAX_SAFE_INTEGER is a constant number that represents the maximum safe integer. This constant has a value of (253 - 1). Here safe refers to the ability to represent integers and to compare them. Syntax: Number.MAX_SAFE_INTEGER Return Value: A constant number. Example 1: Below e
1 min read
Javascript Program for Number of pairs with maximum sum Write a javascript program for a given array arr[], count the number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j. Example: Input : arr[] = {1, 1, 1, 2, 2, 2}Output: 3Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer i
3 min read
JavaScript Program to Add Two Numbers We will explore how to add two numbers in JavaScript. Adding two numbers in JavaScript means combining numeric values together using arithmetic addition, which gives us their total sum. There are several methods that can be used to Add Two Numbers in JavaScript, which are listed below: Table of Cont
2 min read
How to Deal with Large Numbers in JavaScript ? Large numbers are the numbers that can hold huge memory and evaluation time is more than exceeds space and time to process. We can deal with large numbers in JavaScript using the data type BigInt. Advantages:It can hold numbers of large sizes.It performs arithmetic operations.Disadvantages:Consumes
3 min read
JavaScript Number.MAX_VALUE Property Number.MAX_VALUE represents the biggest possible numeric value of a positive number that can be represented in JavaScript. It is the maximum possible positive number representable in JavaScript within float precision. The values greater than Max_VALUE are represented as infinity. Syntax: Number.MAX_
2 min read