ES2015+ and functional programming

With the ES2015+ functionalities, developing functional programs in JavaScript is even easier. Let's consider an example.

Consider we want to find the minimum value of an array. In imperative programming, to perform this task, we simply need to iterate throughout the array and verify that the current minimum value is bigger than the value of the array; if so, we will assign the new minimum value, as follows:

var findMinArray = function(array){ 
  var minValue = array[0]; 
  for (var i=1; i<array.length; i++){ 
    if (minValue > array[i]){ 
      minValue = array[i]; 
    } 
  } 
   return minValue; 
}; 
console.log(findMinArray([8,6,4,5,9])); // outputs 4 

To perform the same task in functional programming, we can use the Math.min function, ...

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.