Jump to content

Recommended Posts

Posted (edited)

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

}

Edited by jtified
Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...