w3resource

JavaScript: Get the highest number from three different numbers

JavaScript Math: Exercise-31 with Solution

Highest of Three Numbers

Write a JavaScript function to get the highest number from three different numbers.

Test Data:
console.log(highest_of_three(-5, 4, 2));
4

Sample Solution:

JavaScript Code:

// Define a function named highest_of_three that returns the highest of three numbers.
function highest_of_three(num1, num2, num3)
{
    // Use Math.max() to determine the maximum value among num1, num2, and num3.
    return Math.max(num1, num2, num3);  
}

// Output the result of finding the highest of -5, 4, and 2 to the console.
console.log(highest_of_three(-5, 4, 2));

Output:

4

Visual Presentation:

JavaScript: Math - Get the highest number from three different numbers.

Flowchart:

Flowchart: JavaScript Math- Get the highest number from three different numbers

Live Demo:

See the Pen javascript-math-exercise-31 by w3resource (@w3resource) on CodePen.


For more Practice: Solve these Related Problems:

  • Write a JavaScript function that determines the highest of three numbers using nested ternary operators.
  • Write a JavaScript function that compares three numbers without using Math.max by sequentially checking each pair.
  • Write a JavaScript function that stores three numbers in an array and uses the reduce method to find the maximum.
  • Write a JavaScript function that compares three numbers using conditional statements and returns the largest value.

Go to:


PREV : Cast Square Root to Integer.
NEXT : Calculate Percentage of a Number.

Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.