If you are in a hurry, better see examples scope visibility in Bacco’s response :)
The main difference is of scope visibility, which Bacco demonstrated in the examples of his answer. To understand how this works, you need to understand how what is called variable Hoisting in Javascript. If you think that variables and functions are created at the point in the code in which they are declared, you are wrong.
First of all, a brief consideration of the functioning of the scope in Javascript: there is the global scope, and there are the function scopes. Each function creates a new scope. If there are nested functions, the innermost ones see the variables of the outermost ones, but not the other way around. A mgibsonbr’s response to closures illustrates this well:
function x(a1) { // "x" tem acesso a "a"
var a2;
function y(b1) { // "y" tem acesso a "a" e "b"
var b2;
function z(c1) { // "z" tem acesso a "a", "b", e "c"
var c2;
Each time a function is invoked, a execution context to it, where the variables it defines are stored. In the case of the function x
above, the execution context contains references to a1
, a2
and y
(respectively, a parameter, a variable, and a function). Now, consider the following example:
function x(a) {
var a = "foo";
function a() {
// nada
}
alert(typeof a);
}
x(10);
What will come out in the Alert? The guy "number"
of the past argument, the type "string"
of the variable, or the type "function"
function? Answer: "string"
(test yourself). At this point, you scratch your head and say,:
But how so?! The function has been declared afterward variable! Should give "Function"!
Yeah, it makes sense what you thought. Turns out the algorithm responsible for Binding of the arguments, variables and functions to the execution context does so in a very specific order, which is the following:
- Arguments
- Job statements
- Variables
If there are equal names between arguments, function statements and variables, they will be superscripted following this order. In our example, a
begins as the number 10
, then it is overwritten by the function, and finally it is overwritten by the variable (before the assignment). This all happens before any code is executed in the function, including the assignment of the value "foo"
to a
. In code, what happens is something like this:
function x(a) {
function a() {
// nada
}
var a; // undefined, por enquanto
a = "foo";
alert(typeof a); // "string"
}
x(10);
Note that the variable declaration is separated from the assignment. It is said that the statement is "erected" or "hoisted" (hoisted) to the top of the scope. See this example:
function f() {
var a = 10;
var b = 10;
function c() {}
var d = function() {};
}
It’s actually interpreted that way:
function f() {
function c() {}
var a = undefined, b = undefined, d = undefined;
a = 10;
b = 10;
d = function() {};
}
Look at that thing! The function c
is available from the start, but the d function is not! That’s because c
was created via function statement, while the function in d
was created via function expression (statements always have a name, and always start with function
as the first thing on the line, disregarding the spaces). If we want to invoke c
before the line where it is declared, there is no problem, since the Hoisting makes it already available:
function f() {
var a = 10;
var b = 10;
c(); // SEM PROBLEMAS!
function c() {}
var d = function() {};
}
The same is not true for d
: there will be only one function in d
after the line where the assignment is made. See:
function f() {
var a = 10;
var b = 10;
d(); // TypeError! Estamos tentando chamar undefined como função
function c() {}
var d = function() {};
}
So, to answer the question: var name = function()
and function name()
are different because the first is a variable statement, to which a function is assigned, while the second is a function statement. This has consequences in relation to when the function will be available to be invoked.
whenever you have comparisons like
a > b
,a <= b
, etc the result of the comparison is alreadytrue
orfalse
:)– Bacco
Function 1 is defined when the script is parsed(parse time) and function 2 when it is executed(run-time)
– tinos