How to use sort method In Javascript
Sorting the numbers in ascending order using sort() method and then selecting the first element (smallest) can also determine the smallest among three numbers.
Example: It creates an array containing the three input numbers, sorts them using a comparison function that orders them numerically, and then returns the first element of the sorted array, which is the smallest number among them.
Javascript
function findSmallest3(num1, num2, num3) { let sortedArray = [num1, num2, num3] .sort((a, b) => a - b); return sortedArray[0]; } // Example usage const num1 = 10; const num2 = 5; const num3 = 12; const smallest = findSmallest3(num1, num2, num3); console.log( "The smallest number is:" , smallest); |
The smallest number is: 5
JavaScript Program to Find the Smallest Among Three Numbers
In JavaScript, finding the smallest among three numbers is a common task in programming. There are multiple approaches to achieving this, each with its advantages and use cases.
There are several approaches in JavaScript to find the smallest among three numbers which are as follows:
Table of Content
- Using Conditional Statements
- Using Math.min() Function
- Using sort method