higher-order functions javascript w3schools code example

Example 1: javascript function variable arguments

function foo() {
  for (var i = 0; i < arguments.length; i++) {
    console.log(arguments[i]);
  }
}

foo(1,2,3);
//1
//2
//3

Example 2: higher order functions javascript

/* Answer to: "higher order functions javascript" */

/*
  Higher order functions are functions that operate on other
  functions, either by taking them as arguments or by returning
  them.
  
  In simple words, A Higher-Order function is a function that
  receives a function as an argument or returns the function as
  output.
  
  For example; Array.prototype.map, Array.prototype.filter and
  Array.prototype.reduce are some of the Higher-Order functions
  built into the language.
  
  For more information, go to:
  https://blog.bitsrc.io/understanding-higher-order-functions-in-javascript-75461803bad
*/

Example 3: javascript function multiple parameters

function sum(...values) {
    console.log(values);
}
sum(1);
sum(1, 2);
sum(1, 2, 3);
sum(1, 2, 3, 4);


function sum(...values) {
    let sum = 0;
    for (let i = 0; i < values.length; i++) {
        sum += values[i];
    }
  
    return sum;
}
console.log(sum(1)); //1
console.log(sum(1, 2)); //3
console.log(sum(1, 2, 3)); // 5
console.log(sum(1, 2, 3, 4)); //10

Example 4: higher order function in javascript

let total = 0, count = 1;
while (count <= 10) {
  total += count;
  count += 1;
}
console.log(total);