Javascript – jquery function inside a function

functionjavascript

Is it possible to have a function within another function like so?

function foo() {

    // do something

    function bar() {

        // do something

    }

    bar();

}

foo();

Best Solution

Yes you can have it like that. bar won't be visible to anyone outside foo.

And you can call bar inside foo as:

function foo() {

    // do something

    function bar() {

        // do something

    }
    bar();

}