JavaScript How a function got passed to another function?

I came across a weird code which I didn't understood. It would be great if I get explanation.

function forEach(array, action) {
  for (var i = 0; i < array.length; i++) {
    action(array[i]);
  }
}

var numbers = [1, 2, 3, 4, 5], sum = 0;
forEach(numbers, function (number) {
  sum += number;
});
console.log(sum);

How the function got passed as parameter ? and how the inner function is getting that number.

Answer:1

In JavaScript, functions are what's called first class citizens (that's not necessarily true for other languages), that means that functions can be treated as data, you can pass functions as arguments, return functions from other functions, and create functions and assign them to variables.

Here's a very simple example:

function callWithoutParams(fn) {
  return fn(); // fn is the passed function.
}

let rand = callWithoutParams(Math.random); // Passing Math.random as a function.
callWithoutParams(() => console.log(42));  // Passing anonymous function

Array.prototype.forEach() accepts a function as an argument, and runs it on each element in the array. So a simpler example is:

[1,2,3,4,5].forEach((element) => console.log(element)); // outputs 1-5 in order.
Answer:2

I am using Google Geocharts and following Google's documentation (https://developers.google.com/chart/interactive/docs/gallery/geochart?hl=en) to setup a chart like this: google.charts.load('current',...

I am using Google Geocharts and following Google's documentation (https://developers.google.com/chart/interactive/docs/gallery/geochart?hl=en) to setup a chart like this: google.charts.load('current',...

  1. google.visualization undefined
  2. google.visualization.datatable undefined
  3. google.visualization.arraytodatatable undefined
  4. typeerror google.visualization is undefined
  5. new google.visualization.datatable() undefined
  6. google.maps.visualization is undefined
  7. google charts google.visualization is undefined
  8. error google.visualization is undefined

I am using a site that allows its users an admin panel for which we can add HTML/CSS/Javascript/Jquery to the site after their DOM is loaded. Since i am a user i can not change what is being loaded ...

I am using a site that allows its users an admin panel for which we can add HTML/CSS/Javascript/Jquery to the site after their DOM is loaded. Since i am a user i can not change what is being loaded ...

  1. remove content page
  2. remove page content sharepoint 2013
  3. remove outdated content page
  4. remove outdated content page google
  5. remove outdated content page google images
  6. google remove content page
  7. remove update content page
  8. remove content facebook page
  9. remove content web page
  10. drupal remove content page
  11. sharepoint remove page content section
  12. genesis remove page content
  13. javascript remove page content
  14. squarespace remove page content
  15. sharepoint remove page content
  16. squarespace remove page content homepage
  17. remove page number from content page
  18. sharepoint 2013 remove page content section
  19. remove-page-content-share

Trying to format/parse the following date stamp from the server into the users local time zone using momentjs library. Added fiddle: http://jsfiddle.net/2hgyb490/ I've tried moment.utc(date) ...

Trying to format/parse the following date stamp from the server into the users local time zone using momentjs library. Added fiddle: http://jsfiddle.net/2hgyb490/ I've tried moment.utc(date) ...

I have a bootstrap modal that trigger additional actions when opened or closed manually. These actions are hooked with show.bs.modal and hide.bs.modal events. Now I want to also be able to open or ...

I have a bootstrap modal that trigger additional actions when opened or closed manually. These actions are hooked with show.bs.modal and hide.bs.modal events. Now I want to also be able to open or ...

  1. open close bootstrap
  2. sidebar open close bootstrap
  3. open close panel bootstrap
  4. open and close bootstrap modal jquery
  5. open close div bootstrap
  6. open close icon bootstrap
  7. open and close bootstrap modal
  8. open close accordion bootstrap
  9. toggle open close bootstrap
  10. open close tab bootstrap
  11. open close button bootstrap
  12. open close side menu bootstrap
  13. bootstrap open close section
  14. bootstrap open close menu
  15. bootstrap open close card
  16. bootstrap open close list
  17. bootstrap open close form