Topic: About declaring variables

Hi there! I'm a beginner when it comes to programming thats why I want to know what ARE the differences of  declaring a variable like this:

function function_name(var1,var2,...,varX)
{

some code

}

TO this:

function function_name()
{

var1;
var2;
varX;
etc.......

some code
}

Last edited by jtified (2009-08-25 00:38:45)

Re: About declaring variables

The difference between the two depends on how you intend to use the Javascript function.

When declaring your variables inside the function, like example #2 above, the variable will always hold one particular value. For example,

function function_name()
{
var1 = 1;
some code;
}

When that variable is inside the function, it will always hold the value of 1 (unless you make changes to the value later in the function).

In example #1, the way the javascript is written allows you to give different values to the variables depending on how you call the function. For example,

function function_name(var1)
{
some code
}

you could call that function, using different values for the variable each time:

function_name(2);
function_name(455);

In line #1, the "var1" variable would equal 2, and in line 2, it would equal 455.

Re: About declaring variables

I understand now. Thank you for that! big_smile