-2

Why do people say "declare the function" when only statements with let, const, or var are declarations? I also get confused because the name of one type of function is a function declaration.

Muhammad
  • 105
  • 7
  • sorta related (not dupe-marking b/c this is JS, and that's C): https://stackoverflow.com/q/1410563/1757964 – APerson Apr 02 '19 at 02:11

2 Answers2

0

is that more clear?

const myFunction = function (a,b) {  return a+b; }

console.log ( myFunction (5,10) );   // 15
console.log ( myFunction (25,30) );  // 55
Mister Jojo
  • 12,060
  • 3
  • 14
  • 33
0

TL;DR:

Javascript developers will often use the terms "function declaration" and "function definition" interchangeably. In some languages these are separate concepts (e.g. C); in Javascript they are not.

Probably when a JS developer says "declare the function" what they mean is "put the function here so you can call it over there."

...

However, so I don't get downvoted to oblivion:

Technically, in Javascript, this is a function declaration:

function foo(bar) {
  // function body goes here
}

Whereas this is a function expression (and also a variable declaration):

const foo = function() {
  // function body goes here
};

Both can be called in the usual way. The results are almost identical, except that the function declaration is hoisted and the const declaration is not.

See here for more on the difference: https://medium.com/@mandeep1012/function-declarations-vs-function-expressions-b43646042052

Achronos
  • 980
  • 6
  • 8