Smallest of Three Numbers Program
In this tutorial, you will write a JavaScript program with a function that takes three numbers as arguments, finds the smallest of these three numbers, and returns the smallest number.
Solution
To find the smallest of three numbers in JavaScript,
- Consider that
num1
,num2
, andnum3
are the three numbers. - If
num1
is less than bothnum2
andnum3
, thennum1
is the smallest. - Else if
num2
is less thannum3
, thennum2
is the smallest. - Else
num3
is the smallest.
Program
1. In the following program, we find smallest of three numbers using if-else-if statement.
function findSmallest(num1, num2, num3) {
if (num1 < num2 && num1 < num3) {
return num1;
} else if (num2 < num3) {
return num2;
} else {
return num3;
}
}
var num1 = 5;
var num2 = 8;
var num3 = 2;
const result = findSmallest(num1, num2, num3);
console.log('Smallest : ' + result);