3

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

Kishore Kumar Korada
  • 1,204
  • 6
  • 22
  • 47
  • 4
    `var` is not a data type like `int` or `double` from a statically typed language. – Jared Smith Dec 21 '14 at 13:05
  • possible duplicate of [What is the function of the var keyword and when to use it (or omit it)?](http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-and-when-to-use-it-or-omit-it) – JJJ Dec 21 '14 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 Dec 21 '14 at 13:18

3 Answers3

8

var 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 commented 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
    };
};
JAAulde
  • 19,250
  • 5
  • 52
  • 63
1

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>'); }
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

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"]
Hasan Al-Natour
  • 1,936
  • 2
  • 14
  • 22
  • You mean to say t has already been allocated a chunk of space in arguments array before we call it? is that the reason we should explicitly use var which helps to create brand new chunk of memory which is redundant in this function? – Kishore Kumar Korada Dec 21 '14 at 14:09
  • every function has a scope , which has everything related to it , at the end a function is an object stored in the memory , arguments in every function are stored inside the array "arguments" and can be retrieved using this array or by its name that is passed to the function , so why use var ! why create a new variable and use more space in the memory . – Hasan Al-Natour Dec 21 '14 at 14:23