Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are arguments in JavaScript not preceded by the var keyword?

Tags:

javascript

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...

like image 947
helpermethod Avatar asked Nov 29 '11 12:11

helpermethod


People also ask

Why VAR is not used in JavaScript?

Scoping — the main reason to avoid var var variables are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } .

Why var keyword is used in JavaScript?

The var keyword is used to declare variables in JavaScript. Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows. Storing a value in a variable is called variable initialization.

Can I declare a variable in JavaScript without VAR?

Variables can be declared and initialize without the var keyword. However, a value must be assigned to a variable declared without the var keyword. The variables declared without the var keyword becomes global variables, irrespective of where they are declared. Visit Variable Scope in JavaScript to learn about it.

How are arguments passed in JavaScript?

Arguments are Passed by Value The parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value.


2 Answers

Most 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 completely 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
like image 154
hugomg Avatar answered Sep 20 '22 12:09

hugomg


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.

like image 31
Richard Ev Avatar answered Sep 21 '22 12:09

Richard Ev