My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

forEach method in JavaScript

Default profile photo
Anonymous
·Mar 15, 2018

Aim is to add the array number i.e 2+2+2-6

var totalArray = [2,2,2];
var sum=0;

function adder(number){
    sum+=number
}
totalArray.forEach(adder());   // replacing this line with totalArray.forEach(adder); is eliminating error

console.log(sum);

While running this code I am getting Undefined is not a function ,here I just want to know whether it is illegal to call a function using parentheses i.e a normal approach which we used to do.