Sign in
Log inSign up

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.

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct