Javascript forEach code to find large and small number from Array

By | August 24, 2022

Javascript forEach code to find large and small number from Array. Here is the Javascript code which help you to find biggest and smallest number from a given Array. Here we used forEach method loop the number of given arrays and find smallest and largest number.

Java Script Code:

let large,small,i;
let arr = [5, 4, 10, 68, 55];

large=small=arr[0];
arr.forEach((val) =>
{
if(val>large)
large=val;

if(val<small)
small=val;
})

console.log(“The smallest element is ” + small );
console.log(“The largest element is ” + large );

Output:

The smallest element is 4
The largest element is 68

Leave a Reply

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