How to store function value in variable javascript
I'm learning JavaScript at the moment and I don't quite understand when to write a function into a variable. Show For instance, both of the following code blocks do the exact same thing in Node.js:
and
Which is the best method to do according to best practices, and why? asked Mar 8, 2012 at 13:36
4 Usually I'll only use a
Otherwise, for most purposes (assuming it's not a callback or a modifier) declaring a function "classically" is usually acceptable. answered Mar 8, 2012 at 13:43
Brad ChristieBrad Christie 98.8k16 gold badges149 silver badges198 bronze badges 5 I default to the non-variable
answered Mar 8, 2012 at 13:56
SuprSupr 18k3 gold badges29 silver badges35 bronze badges Here is an explaination: There is a distinction between the function name and the variable the function is assigned to:
https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope answered Mar 8, 2012 at 13:46
ChurkChurk 4,4465 gold badges21 silver badges37 bronze badges 3 I see that nobody mentioned (directly and practically - which seems this
question is about) the reason for which I personally find most useful for storing functions in variables. That reason is dealing with a complex logic that requires a lot of choices (e.g.
If we tried to call functions directly, the code would get much more messy and get the messier the more complex the logic is. answered Sep 18, 2019 at 17:15
EggonEggon 1,7442 gold badges13 silver badges31 bronze badges According to John Resig's JavaScript as a First Language article, your first code block is considered as best practice. answered Mar 8, 2012 at 13:42 1 I personally have encountered certain issues with calling local functions (functions declared inside other functions) when using non-variable syntax in some versions of IE (most likely IE8 or lower) while variable syntax did work as expected. Given that functions should not be declated in global namespace, most of functions are local, and therefore it makes sense to use variable syntax for functions always. answered Mar 8, 2012 at 13:57
Marat TanalinMarat Tanalin 13.7k1 gold badge34 silver badges51 bronze badges I see some different opinions here, but most of them are more based on what you think is better or not, but i don't see the technical reasons on when to use one or another, but for sure there are technical restrictions or advantages in use one declaration formula or another. I'm really a beginner with javascript and I don't feel confident to advise on it, but I will propose a case in which storing a function in a variable is not functional. In the code below, defining an Angular filter, if I define the function inside a variable, then I can't successfully call it from the filter method. Could anyone explain me the technical reason on this? See the code (commented is not working):
Thank you in advance! answered Jul 15, 2016 at 8:22
AtomicNationAtomicNation 1811 gold badge2 silver badges6 bronze badges How do you store the value of a function in a variable in JavaScript?Functions stored in variables do not need function names. They are always invoked (called) using the variable name. The function above ends with a semicolon because it is a part of an executable statement.
How do you put a function into a variable?we put the function in a variable if inside the function block we use the return method: var multiplyTwo = function (a) { return a * 2; };
What does () => mean in JavaScript?It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.
Can JavaScript function return value?When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller. For example, the following function returns the square of its argument, x , where x is a number. If the value is omitted, undefined is returned instead.
|