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…
Continue reading “JavaScript Arrays — Finding The Minimum, Maximum, Sum, & Average Values”