links: [[JS MOC]] --- ### Function Declaration vs Expression #### Inputs JS Functions can receive parameters from zero to upward ``` function greeting(myName) { console.log(`Hello, ${ myName }!`); } greeting("Subramanya") ``` #### Return JS Functions can return values using *return* keyword ``` function greeting(myName) { return `Hello, ${ myName }!`; } var msg = greeting("Subramanya") console.log(msg) // Hello Subramanya ``` You can only *return* a single value #### Function Declaration ``` foo() // works function foo() { // ... } foo() // works ``` Because of function hoisting, the function declared this way can be called both after and before declaration. The association between the identifier `foo` and function value happens during compile phase of the code #### Function Expression 1. **Named Function Expression** ``` var foo = function bar() { // ... } ``` 2. **Anonymous Function Expression** ``` var foo = function(){ // ... } ``` `foo()` can be called only after creation - the above function is not associated with it's identifier `foo` until that statement during runtime **My recommendation:** Use Function Expression, because it makes it clear that `foo` is a variable containing a function value --- tags: #javascript [source](https://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip)