65

I'm learning JavaScript at the moment and I don't quite understand when to write a function into a variable.

For instance, both of the following code blocks do the exact same thing in Node.js:

 var onReq = function(req, res) {
   res.write('Hello');
 };

 http.createServer(onReq).listen(3000);

and

function onReq(req, res) {
   res.write('Hello');
 }

 http.createServer(onReq).listen(3000);



Which is the best method to do according to best practices, and why?

4
  • for this question you need more concentration on String variables and integer and float variables; Mar 8, 2012 at 13:47
  • duplicate: stackoverflow.com/questions/336859/… Mar 8, 2012 at 14:12
  • 2
    The second form has the advantage that if for example it's a terribly long function, you can move it to the bottom of the current variable scope to get it out of the way, but still use it higher up. This can come in handy. You can even put it below a final return statement, and it'll still work.
    – user1106925
    Mar 8, 2012 at 14:39

7 Answers 7

35

Usually I'll only use a var funcName = function(){} when I would need to redefine the action(s) for that function later on. For example:

var foo = function(a){ return a * 2; }
var bar = foo(2);

foo = function(a){ return a / 2; }

bar = foo(bar);

Otherwise, for most purposes (assuming it's not a callback or a modifier) declaring a function "classically" is usually acceptable.

5
  • I see what you mean, but it's still better to do the former method of declaring function's I've stated right?
    – imjp
    Mar 8, 2012 at 13:50
  • What would be reason to redefine a function, as opposed to just writing another function?
    – Mr. J
    Oct 2, 2021 at 2:34
  • 1
    @Mr.J never had to change a computation or a validation function? Or want to change the behavior of a function? Oct 3, 2021 at 3:33
  • @BradChristie When I've wanted to change the behaviour of a function, I write another function as opposed to re-writing the existing one. Not saying your wrong btw, I'm just curious to know a use-case where rewriting an existing function is better than making a new one
    – Mr. J
    Oct 6, 2021 at 10:23
  • @Mr.J understood. Coming from c# world, I don't find the approach too dissimilar from leveraging delegates or Func<>/Action<> methods. Admittedly it's an infrequent occurrence, but feasible nonetheless. Oct 7, 2021 at 14:29
17

I default to the non-variable function onReq(){} version. It's not a concious decision I've made, but thinking about it brings forth these arguments:

  • It looks cleaner.
  • It is conceptually simpler: it's just a function, while the other is a function and a variable. It's a small thing, but I find it valuable none the less.
  • It assures me that onReq will always refer to that function body - one less thing to consider when reading the code. Sort of like marking a variable as final in Java.
  • Keeps me from "accidentally" replacing the function, causing unintended side effects elsewhere.
7

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's) to determine further course of action for which different functions should be called, possibly with different sets of inputs as well. It makes code cleaner when that further course of action is launched in just one place, at the end of our logic.

function fooA (input) {...};
function fooB (input) {...};
let chosenHandler;
let chosenInput;

// Here we establish which function is relevant
if (someCondition) chosenHandler = fooA;
else chosenHandler = fooB;

// Here we establish which parameter should be used
if (someOtherCondition) chosenInput = 'First input';
else chosenInput = 'Second input';

// Call chosen function with chosen parameters - one place, nice and clean
chosenHandler(chosenInput);

If we tried to call functions directly, the code would get much more messy and get the messier the more complex the logic is.

3

Here is an explaination:

There is a distinction between the function name and the variable the function is assigned to:

  • The function name cannot be changed, while the variable the function is assigned to can be reassigned.
  • The function name can be used only within the function's body. Attempting to use it outside the function's body results in an error (or undefined if the function name was previously declared via a var statement).

https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope

3
  • Just a quick addendum. The function's name in a fn declaration (as MDN uses the term) can be changed - just assign it to a new variable to get another reference to the function. The function name can also be reassigned to another function (or something else entirely) later in the code. Another difference is that a function declaration will hoist the assignment to the top of the scope, whereas using a variable will only hoist the variable's declaration.
    – meloncholy
    Mar 8, 2012 at 14:12
  • Neither of your points seem correct. For the first one it's a little hard to see what you're saying, but in both cases the variable referencing the function can be reassigned. And the second point isn't right. The function name can be used outside the function body as shown in the question. You're thinking of a named function expression, which is not shown in the question.
    – user1106925
    Mar 8, 2012 at 14:20
  • Via one of the duplicate links above, kangax's Named function expressions demystified covers this in a lot more detail and with a lot more subtlety (e.g. pointed out that my comment on hoisting behaviour can actually vary between browsers). Definitely worth checking out.
    – meloncholy
    Mar 8, 2012 at 14:36
1

According to John Resig's JavaScript as a First Language article, your first code block is considered as best practice.

1
  • 9
    Don't be too influenced by that. There are lots of times where it's much more desirable to use a function declaration, and take advantage of the fact that it is hoisted to the top of your code. Sad that he suggests // Don't do this: function getData() { } because it makes it easier to teach functions as an object. Making it easier to teach is a poor reason to start beginners off with the idea that there's something wrong with it.
    – user1106925
    Mar 8, 2012 at 14:30
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.

0

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

angular
    .module('loc8rApp')
    .filter('formatDistance', formatDistance);

function formatDistance(){
        return function (distance) {
        var numDistance, unit;
        if (distance && _isNumeric(distance)) {
         if (distance > 1) {
            numDistance = parseFloat(distance).toFixed(1);
            unit = 'km';
          } else {
            numDistance = parseInt(distance * 1000,10);
            unit = 'm';
          }
          return numDistance + unit;
        } else {
          return "?";
        }
      };
};

var _isNumeric = function (n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
};

/*
var formatDistance = function () {
  return function (distance) {
    var numDistance, unit;
    if (distance && _isNumeric(distance)) {
      if (distance > 1) {
        numDistance = parseFloat(distance).toFixed(1);
        unit = 'km';
      } else {
        numDistance = parseInt(distance * 1000,10);
        unit = 'm';
      }
      return numDistance + unit;
    } else {
      return "?";
    }
  };
};
*/

Thank you in advance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.