Find largest among 3 numbers

In this tutorial we learn how to find largest among 3 numbers. This is also a javascript solution to our exercise find greatest among 3 numbers.

Find largest among 3 numbers -defining variables

Define three variables and assign them values.
Also define 2 more variables named ‘greater’ and ‘greatest’

Find largest among 3 numbers – defining the function

Define a function ‘greatestof3’ that will return us the largest among 3 numbers.

 

Find largest among 3 numbers – comparing first 2 values

Now compare the value of num1 with the value of num2 using a if/else statement and store it in the variable ‘greater’. This will store the number which is greater between num1 and num2

 

Find largest among 3 numbers – comparing the greater between first two values with the third number

Now compare the value of ‘greater’ with num3 using if/else statement and store it in the variable ‘greatest’. This will store the greatest among 3 numbers. The function will return the ‘greatest’.

 

Find largest among 3 numbers – Calling the function and displaying the largest number

Call the function on windows.load event and display the output using console.log or window.alert

Find largest among 3 numbers – complete solution