4. Custom Functions

4.3. Differences between two methods in declaring functions


          So, what is the difference between the function as a declaration and the function as an expression?

          Function as a declaration could be “hoisted” while as an expression cannot.  It means that you can use a function or make a function call before it is declared. Thus, the example below will still work but not for function as an expression.

 

/* calling the function addTwoNumbersv2, the result of this function is 9 */
document.write(addTwoNumbersv2(3, 6));
/* this example will return the sum of a given two arguments */
function addTwoNumbersv2(num1, num2)

   return num1 + num2;
}

          Function as an expression has several advantages, like creating a callback and when you would like to test immediately the result of lines of codes without giving it a name.  For more information about function as an expression refer to the web page Function Expression using the URL below: 

https://developer.mozilla.org/en-US/docs/web/JavaScript/Reference/Operators/function