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!)

5
  • Possibly relevant: stackoverflow.com/questions/1410563/… Commented Mar 19, 2014 at 11:50
  • Please give me some examples of declaration and in JS! Commented Mar 19, 2014 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. Commented Mar 19, 2014 at 11:57
  • function definition - body of function without name and function declaration - only name of function, but not body? Commented Mar 19, 2014 at 12:31
  • See also What is the difference between function declaration Vs. definition in JavaScript? Commented Apr 2, 2024 at 22:28

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

"Official term"? Can you back that up with a link to or quote from official documentation?
declaration and definitions synonims in JS?
@igor_rb I've never heard "definition" before, but they seem to be synonyms here, yes.
@JamesDonnelly: The spec specifies FunctionDeclaration and FunctionExpression for defining functions.
In this book declaration and definition words use very often, and it gives some misunderstand for me. Thanks!
|
2

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.