1

What is difference between function declaration and function definition in JavaScript? I read chapter 5.3.2 from this book

When nested, however, function declarations may only appear at the top level of the function they are nested within. That is, function definitions may not appear within if statements, while loops, or any other statements.

but don't understand difference between declaration and definition... Please, explain it ( note: I understand difference between function expression and function declaration!)

rdonatoiop
  • 1,185
  • 1
  • 14
  • 28
igor_rb
  • 1,821
  • 1
  • 19
  • 34
  • Possibly relevant: http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration – James Donnelly Mar 19 '14 at 11:50
  • Please give me some examples of declaration and in JS! – igor_rb Mar 19 '14 at 11:54
  • `var x = function() { return true; }`. I guess you could say here `x = function` is the declaration, and `return true;` is the definition of that function. – James Donnelly Mar 19 '14 at 11:57
  • function definition - body of function without name and function declaration - only name of function, but not body? – igor_rb Mar 19 '14 at 12:31

3 Answers3

3

The important thing is to distinguish two cases:

  • var f1 = function() {...} This is often referred to as a function expression.

  • function f2() {...} This is often referred to as a function declaration.

Whatever the names you give to it, var f1 will be hoisted to the top of the wrapping function (but f1 will be assigned the value of the function only at the line you have written it). However the whole function declaration of f2 will be hoisted to the top of the wrapping function.

There is a great post that explains this more in details here.

Greg
  • 3,370
  • 3
  • 18
  • 20
2

In this case, they are just using two different terms to refer to the same thing. The official* term is "function declaration".

Source for official is page 98 of the ECMAScript standard.

Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • "Official term"? Can you back that up with a link to or quote from official documentation? – James Donnelly Mar 19 '14 at 11:51
  • declaration and definitions synonims in JS? – igor_rb Mar 19 '14 at 11:52
  • @igor_rb I've never heard "definition" before, but they seem to be synonyms here, yes. – Scimonster Mar 19 '14 at 11:53
  • @JamesDonnelly: The [spec](http://www.ecma-international.org/ecma-262/5.1/#sec-13) specifies FunctionDeclaration and FunctionExpression for defining functions. – RobH Mar 19 '14 at 11:55
  • In this book declaration and definition words use very often, and it gives some misunderstand for me. Thanks! – igor_rb Mar 19 '14 at 11:55
  • @RobH function declaration is subset for function definition? – igor_rb Mar 19 '14 at 11:58
  • @igor_rb Yeah, both function declarations and expressions are function definitions, according to the spec. – Scimonster Mar 19 '14 at 11:59
  • @igor_rb - I honestly don't know. I think you'd have to ask the authors about the distinction they're making. You can definitely define a function within an `if`. e.g. `var a; if (true) { a = function() { return 'worked' }; } else { a= function() { return; }; } console.log(a());` – RobH Mar 19 '14 at 12:03
0
function isEven(num){
     if(num%2 === 0){
return true;
}else{
return false;
}
}

In the above code function isEven(num) is called as function declaration, its like in that line of code you declare that isEven you are using is a function.

And the remaining part of the code inside the curly braces is called function definition. Where you provide defintion for the function isEven.

In js, we don't seperate the declaration and defintion, because we have hoisting. Hoisting makes your function declaration & definition to be hoisted to the top, so you can use the function even before defining it.

In other programming languages like 'C', you don't have hoisting. So the declarations of functions are made before usage and defintions can be placed where ever we want, inside the file.

And thus the reason for two names and what they mean.

Darwin
  • 11
  • 1