Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript scope of function declarations

Tags:

The var keyword in javascript causes a variable to be stored in the local scope. Without var variables belong to the global scope. What about functions? It's clear what happens when functions are declared like variables

var foo = function() {...}

but what scope does

function foo() {...} 

belong to?

EDIT: I realized I didn't ask quite the right question so as a follow up. In the outer most nesting is there a difference between the above two declarations and the following declaration?

foo = function() {...}
like image 834
user1816847 Avatar asked Feb 02 '13 18:02

user1816847


People also ask

What is the scope of a function JavaScript?

JavaScript has function scope: Each function creates a new scope. Variables defined inside a function are not accessible (visible) from outside the function. Variables declared with var , let and const are quite similar when declared inside a function.

Are function declarations block scoped?

Note that the block-scoped const c = 2 does not throw a SyntaxError: Identifier 'c' has already been declared because it can be declared uniquely within the block. In strict mode, function declarations inside blocks are scoped to that block and are hoisted.

What is function declaration in JS?

The function declaration (function statement) defines a function with the specified parameters. You can also define functions using the Function constructor and a function expression.

What is the scope of a function?

When you declare a program element such as a class, function, or variable, its name can only be "seen" and used in certain parts of your program. The context in which a name is visible is called its scope. For example, if you declare a variable x within a function, x is only visible within that function body.


2 Answers

It belongs to the current scope, always. For example:

// global scope

// foo is a global function
function foo() {

    // bar is local to foo
    function bar() {

    }

}

Regarding your second question, this:

foo = function() {...}

is an anonymous function expression assigned to a global variable (unless you're running is strict mode, then foo would be undefined). The difference between that and function foo() {} is that the latter is a function declaration (versus a variable declaration, which is assigned an anonymous function expression).

You might be interested in this excellent article about function declarations and function expressions: Named function expressions demystified.

like image 66
bfavaretto Avatar answered Dec 28 '22 07:12

bfavaretto


Function declarations are always local to the current scope, like a variable declared with the var keyword.

However, the difference is that if they are declared (instead of assigned to a variable) their definition is hoisted, so they will be usable everywhere in the scope even if the declaration comes in the end of the code. See also var functionName = function() {} vs function functionName() {}.

like image 40
Bergi Avatar answered Dec 28 '22 05:12

Bergi