I know it seems silly question but I'm actually confused with following scenario which as follows...
I understood that for every variable we need to specify datatype as var or though it's not a good practice we can go without var as well. For instance
We can either use
var name = "kishore";
or simply
name = "kishore";
But, when it es to variables that we are passing as parameters in functions, Why doesn't they accept var? If I'm not wrong, I think those are just like other variable which has to store some value that can be accessible in that function. If I specify var it gives error. I just want to know what's going on there..?
I know it seems silly question but I'm actually confused with following scenario which as follows...
I understood that for every variable we need to specify datatype as var or though it's not a good practice we can go without var as well. For instance
We can either use
var name = "kishore";
or simply
name = "kishore";
But, when it es to variables that we are passing as parameters in functions, Why doesn't they accept var? If I'm not wrong, I think those are just like other variable which has to store some value that can be accessible in that function. If I specify var it gives error. I just want to know what's going on there..?
Share Improve this question edited Jan 13, 2015 at 1:47 Kishore Kumar Korada asked Dec 21, 2014 at 12:57 Kishore Kumar KoradaKishore Kumar Korada 1,2627 gold badges24 silver badges52 bronze badges 3-
4
var
is not a data type likeint
ordouble
from a statically typed language. – Jared Smith Commented Dec 21, 2014 at 13:05 - 1 possible duplicate of What is the function of the var keyword and when to use it (or omit it)? – JJJ Commented Dec 21, 2014 at 13:11
-
@Juhana It's close to being a duplicate, but varies enough that I think it stands on its own. The linked question and accepted answer do not mention parameters in context of the use of
var
. – JAAulde Commented Dec 21, 2014 at 13:18
3 Answers
Reset to default 8var
is not a datatype, but a keyword/operator used to declare a variable in the appropriate scope. Function parameters are already scoped to the function for which they are parameters and there is no changing that. Thus the use of var
would be redundant and was not required.
Some mented code samples:
// A globally accessible function
var f1 = function (p1) { // p1 automatically scoped to f1 and available to all inner functions (like f2 below)
// A variable available in the scope of f1 and all inner functions (like f2 below)
var v1;
// f1, p1, v1, and f2 can be reached from here
var f2 = function (p2) { // p2 automatically scoped to f2
// A variable available in the scope of f2
var v2;
// f1, p1, v1, f2, p2, and v2 can be reached from here
};
};
The var
keyword is for declaring new variables.
Parameters are different. They already exist when the function is called, the declaration in the function header just gives names to the already existing values.
The arguments
collection contains the values that are passed to the function, and you can access the parameters there even if they are not declared as parameters in the function header.
Example:
function test1(a, b, c) {
show(a);
show(b);
show(c);
}
function test2() {
show(arguments[0]);
show(arguments[1]);
show(arguments[2]);
}
test1(1, 2, 3);
test2(1, 2, 3);
// show in Stackoverflow snipper
function show(n) { document.write(n + '<br>'); }
The Parameters are already stored in the function scope in the arguments array , so you don't need to use var :
function fun(t){
console.log(arguments)
};
fun('3');
Output is :
["3"]