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

3
  • 4
    var is not a data type like int or double from a statically typed language. 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)? 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. Commented Dec 21, 2014 at 13:18

3 Answers 3

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
    };
};
Sign up to request clarification or add additional context in comments.

3 Comments

So, If I define a variables as var val1 = 2, var val2 = 5 and invoke a function as addValues(a, b) by passing val1 and val2, then you mean to say a is scoped to val1 and b is scoped to val2?
No. Variables are scoped to functions, not values.
Okay, Now it's making sense to me. Thank You :)
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>'); }

Comments

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"]

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.