JavaScript Arrays — Finding The Minimum, Maximum, Sum, & Average Values

  • Math.min() , as expected, returns the smallest of any set of given numbers.
  • The problem with Math.min() is it expects each parameter to be input separately and cannot take an array of multiple numbers as input.
  • min([1,2,3,4])// NaNAs you can see, Math.min() only works when each number is input as a separate parameter.Because of this, we need to make use of the …spread operator.
  • The spread operator allows an expression to be expanded in places where multiple elements/variables/arguments are expected.When we use the spread operator within Math.min() it will expand, or spread out, our array and insert each variable as a separate parameter into Math.min()!
  • In other words: Math.min(…[1,2,3,4]) is the same as Math.min(1,2,3,4)Finally, all I’ve done with the code above is turned it into a function expression utilizing arrow functions.

In this article we’ll explore four plug and play functions that allow you to easily find certain values in an arrays of numbers. Specifically we’ll explore the following: Math is a built in object in…

In this article we’ll explore four plug and play functions that allow you to easily find certain values in an arrays of numbers. Specifically we’ll explore the following:

is it expects each parameter to be input separately and cannot take an array of multiple numbers as input. To illustrate this, take a look at the code below:

only works when each number is input as a separate parameter.

Because of this, we need to make use of the …spread operator. The spread operator allows an expression to be expanded in places where multiple elements/variables/arguments are expected.

Finally, all I’ve done with the code above is turned it into a function expression utilizing arrow functions. If arrow functions scare you, simply remember that the two functions below are the same:

If you need a refresher on arrow function syntax, check out my previous article: JavaScript — Learn & Understand Arrow Functions .

Again, don’t let arrow functions scare you. The code above is identical to this function:

functions.

In other words, we are simply going to start with zero and one by one add each value of the array to our initial value until we’ve looped through the entire array. When done, the accumulator value will be returned

function explained above. If you think about it, this makes sense. Finding the average value of a group of numbers is as easy as summing all of the…

JavaScript Arrays — Finding The Minimum, Maximum, Sum, & Average Values