Skip to content

Find the second smallest number in array JavaScript | Example code

  • by

If performance doesn’t bother you then simply sort the array and get the 1 index value of the array to get the second smallest number in array JavaScript.

Find the second smallest number in array JavaScript Example

HTML example code:

Sort method will update existing array in increasing order.

<!DOCTYPE html>
<html>
<body>
	
	<script>
		var arr = [2, 1, 9, 5, 7];

		var smallest = arr.sort((a, b) => a - b);

		alert(smallest[1]);
	</script>

</body>
</html>

Output:

Find the second smallest number in array JavaScript

Note: Array index is start from 0.

Do comment if you have any doubt and suggesiton on this JS array topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *