How to Find the Largest Number in an Array in JavaScript?

Do you want to know what the largest number in JavaScript is and how to find the largest number in an array?

In this article, you’ll discover the largest number and also learn how to find the largest number in an array using JavaScript.

Sounds great, right? So what are you waiting for? Let’s get started!

What is the largest number in JavaScript?

There are two types of numbers In JavaScript, the Number and BigInt.

The Number type is the most commonly used. It’s a 64-bit floating point number, following the IEEE 754 standard.

The largest number in JavaScript that can be represented exactly (meaning without any loss of precision) is known as Number.MAX_SAFE_INTEGER, which equals 2^53-1, or +/- 9,007,199,254,740,991.

However, the absolute largest number that can be represented by the Number type is Number.MAX_VALUE, which is approximately 1.7976931348623157e+308. If you try to use a number larger than this, JavaScript will represent it as Infinity.

Now, if you need to work with really large numbers, JavaScript has another type called BigInt. This type was introduced in ES2020 and can represent integers of any length without losing precision. For instance, you could use BigInt to accurately represent a number like 1 followed by 10,000 zeros!

Solutions on how to find the largest number in an array in JavaScript

The following are the several ways to find the largest number in an array in JavaScript:

Solution 1: Use the Math.max() function with the spread operator (…)

To find the largest number in an array, you can use the Math.max() function with the spread operator. The Math.max() returns the largest of zero or more numbers.

Meanwhile, the spread operator (…) is used to expand the elements of our array into arguments of the Math.max() function.

Here’s an example:

let SampleArray = [10, 20, 30, 40, 50];
let LargestArray = Math.max(...SampleArray); ✅
console.log(LargestArray); 

Output:

50

Solution 2: Use the Math.max() function with the apply() method

You can also use Math.max() but in conjunction with the apply() method.

The apply() method calls a function with a given value and arguments provided as an array (or an array-like object).

Here’s an example:

let SampleArray = [11, 22, 33, 44, 55];
let LargestArray = Math.max.apply(null, SampleArray); 
console.log(LargestArray);

In this case, we’re passing null as this value (since Math.max() doesn’t use this), and our array as the arguments.

Output:

55

Solution 3: Use the reduce() method

You can also use the reduce() method. This method applies a function against an accumulator (in this case, a) and each element in the array (from left to right) to reduce it to a single output value.

The function we’re using is again Math.max(), so we’re essentially comparing each element in the array to find the maximum value.

Here’s an example:

let SampleArray = [1000, 200, 300, 400, 500];
let LargestArray = SampleArray.reduce(function(x, y) { 
return Math.max(x, y);
});
console.log(LargestArray);

Output:

1000

Conclusion

In conclusion, we have explored the concept of the largest number in JavaScript and how to find the largest number in an array.

We learned that JavaScript has two types of numbers: Number and BigInt. The Number type can represent numbers up to approximately 1.7976931348623157e+308, and for exact integers, it can go up to 2^53-1 or +/- 9,007,199,254,740,991.

For larger numbers, JavaScript provides the BigInt type.

We also discussed three different methods to find the largest number in an array in JavaScript: using the Math.max() function with the spread operator (…), using the Math.max() function with the apply() method, and using the reduce() method.

We hope this article has provided you with enough information to understand how to find the largest number in JavaScript array.

If you want to explore more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment