Named Functions

suggest change

Functions can either be named or unnamed (anonymous functions):

const namedSum = function sum (a, b) { // named
    return a + b;
}

const anonSum = function (a, b) { // anonymous
    return a + b;
}

namedSum(1, 3);
anonSum(1, 3);

4 4

But their names are private to their own scope:

const sumTwoNumbers = function sum (a, b) {
    return a + b;
}

sum(1, 3);

Uncaught ReferenceError: sum is not defined

Named functions differ from the anonymous functions in multiple scenarios:


Function declarations are hoisted

A function declaration can be called before its line. A function expression, whether it has an internal name or not, cannot: only the variable is hoisted, and until the assignment it is not a function.

foo();
var foo = function () { // function expression
    console.log('bar');
}

Uncaught TypeError: foo is not a function

foo();
function foo () { // function declaration
    console.log('bar');
}

bar

Named Functions in a recursive scenario

A recursive function can be defined as:

const say = function (times) {
    if (times > 0) {
        console.log('Hello!');

        say(times - 1);
    }
}

//you could call 'say' directly, 
//but this way just illustrates the example
const sayHelloTimes = say;

sayHelloTimes(2);

Hello! Hello!

What if somewhere in your code the original function binding gets redefined?

let say = function (times) {
    if (times > 0) {
        console.log('Hello!');

        say(times - 1);
    }
}

const sayHelloTimes = say;
say = "oops";

sayHelloTimes(2);

Hello! Uncaught TypeError: say is not a function

This can be solved using a named function

// The outer variable can even have the same name as the function
// as they are contained in different scopes
let say = function say (times) {
    if (times > 0) {
        console.log('Hello!');

        // this time, 'say' doesn't use the outer variable
        // it uses the named function
        say(times - 1);
    }
}

const sayHelloTimes = say;
say = "oops";

sayHelloTimes(2);

Hello! Hello!

And as a bonus, the inner name cannot be replaced. In non-strict code the assignment is ignored; in strict mode, including modules, it throws a TypeError:

let say = function say (times) {
    // Ignored in non-strict code; TypeError in strict mode.
    say = undefined;
    
    if (times > 0) {
        console.log('Hello!');

        // this time, 'say' doesn't use the outer variable
        // it's using the named function
        say(times - 1);
    }
}

const sayHelloTimes = say;
say = "oops";

sayHelloTimes(2);

Hello! Hello!

The name property of functions

Before ES6, named functions had their name properties set to their function names, and anonymous functions had their name properties set to the empty string.

var foo = function () {}
console.log(foo.name); // outputs ''

function foo () {}
console.log(foo.name); // outputs 'foo'

Post ES6, named and unnamed functions both set their name properties:

var foo = function () {}
console.log(foo.name); // outputs 'foo'

function foo () {}
console.log(foo.name); // outputs 'foo'

var foo = function bar () {}
console.log(foo.name); // outputs 'bar'

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents