1
In Javascript it is possible to write functions with or without the use of the keyword var
, see below two functions that illustrate this situation:
var soma = function(a, b) {
return a + b;
}
window.alert("Soma de 10 + 5 eh igual a: " + soma(10, 5));
subtrai = function(a, b) {
return a - b;
}
window.alert("Subtracao de 20 - 10 eh igual a: " + subtrai(20, 10));
Note that the function soma()
uses the keyword var
and the function subtrai()
does not use it. Therefore, I would like to know what is the purpose of using the keyword var
when it is used in job statements and whether the use of it or not implies something?
What is the difference between the functions var name = Function and Function name?
– rray
@rray had not noticed rsrs.
– gato
Note that this second case is different from doing
function subtrai
- both examples create an anonymous function and assign it to a variable. The difference is that in the second case the variablesubtrai
is global, was created without the use ofvar
. More information in that other question.– mgibsonbr