This may be a silly question, but why are function arguments in JavaScript not preceded by the var keyword?
Why:
function fooAnything(anything) {
return 'foo' + anyThing;
}
And not:
function fooAnything(var anything) {
return 'foo' + anyThing;
}
I have a feeling the answer is because that's what the Spec says
but still...
This may be a silly question, but why are function arguments in JavaScript not preceded by the var keyword?
Why:
function fooAnything(anything) {
return 'foo' + anyThing;
}
And not:
function fooAnything(var anything) {
return 'foo' + anyThing;
}
I have a feeling the answer is because that's what the Spec says
but still...
-
Why do you want the keyword there?
var
declares variables, parameters are (a bit) different. There is no point of the keyword there, it adds no value. – user1046334 Commented Nov 29, 2011 at 12:14 - isn't that a reason good enough? what additional information will var give you? besides, you save 4 chars everytime you declare a function var which you can put on making the variable name longer for clarity :). – Asken Commented Nov 29, 2011 at 12:14
- 1 Because ECMA-262 says formal parameters, like declared variables, are made named properties of the local variable object before any code is executed. So a formal parameter is more or less declared by default. – RobG Commented Nov 29, 2011 at 12:24
- 2 It's not "because that's what the Spec says" - there is a reason why the spec says that: because it makes sense to do it that way. Requiring "var" would be redundant, and making it optional would be confusing. (JavaScript does have some other features that in my opinion are pretty arbitrary and fall into the "because that's what the spec says" category.) – nnnnnn Commented Nov 29, 2011 at 12:37
4 Answers
Reset to default 9Most dynamicaly-typed programming languages don't have explicit vars in the argument list. The purpose of the var keyword is to differentiate between "I am setting an existing variable" and "I am creating a new variable" as in
var x = 17; //new variable
x = 18; //old variable
(Only few languages, like Python, go away with the var pletely but there are some issues with, for example, closures and accidental typos so var is still widespread)
But in an argument list you cannot assign to existing variables so the previous ambiguity does not need to be resolved. Therefore, a var declaration in the arguments list would be nothing more then redundant boilerplate. (and redundant boilerplate is bad - see COBOL in exibit A)
You are probably getting your idea from C or Java but in their case type declarations double up as variable declarations and the cruft in the argument lists is for the types and not for the declarations.
We can even get away with a "typeless, varless" argument list in some typed languages too. In Haskell, for example, types can be inferred so you don't need to write them down on the argument list. Because of this the argument list consists of just the variable names and as in Javascript's case we don't need to put the extraneous "let" (Haskell's equivalent to "var") in there:
f a b = --arguments are still variables,
-- but we don't need "let" or type definitions
let n = a + b in --extra variables still need to be declared with "let"
n + 17
It would be a redundant use of the var
keyword. Items that appear in the parentheses that follow a function name declaration are explicitly parameters for the function.
The var
keyword declares the scope of the variable. A function argument also introduces the scope for that argument. Hence there's no need for it, since it serves the same function.
I think the question es up because we're used to seeing function bla (int i)
in many languages. The same, syntactically, as the int i;
somewhere in the function body to declare a variable. The two int
s are however not doing the same; the first defines the type, the second defines type and the scope. If you don't have to declare the type, the scope-declaration still needs to happen (which is why we have var
in the second case) but there is no information needed in front of arguments.