25

I know that a parameter is a variable passed to a function and gives value to the argument in the function, but I'm having trouble understanding:

What is the main difference between "arguments" and "parameters" in javascript?

3
  • 2
    Different words for the same thing. Commented Oct 13, 2012 at 15:21
  • there's something called arguments also. have a look. Commented Oct 13, 2012 at 15:31
  • Great answers guys! Thanks for taking me out the the doubt. Commented Oct 13, 2012 at 15:38

6 Answers 6

75

The parameters are the aliases for the values that will be passed to the function. The arguments are the actual values.

var foo = function( a, b, c ) {}; // a, b, and c are the parameters

foo( 1, 2, 3 ); // 1, 2, and 3 are the arguments
Sign up to request clarification or add additional context in comments.

4 Comments

If you called foo like foo( x, y, z), are x, y, and z the arguments or are the values of x, y, and z the arguments? I don't know if the answer is important, I'm just curious.
@DanielKaplan The variables are the same as their values. Calling foo(x, y, z) is the same as foo(1, 2, 3) for instance.
Sorry, my question wasn't clear. I was asking about the appropriate terminology, specifically about the term "argument" in this context. When you call a function by passing in variables, are the variable names the "arguments" or the "variable values"?
@DanielKaplan The variable names are the arguments.
5

When you define a function, the variables that represent the values that will be passed to it for processing are called parameters. For example, the following function definition has one parameter called $number:

function doubleIt($number) {
    return $number *= 2;
}

However, when you use a function, the value you pass to it is called an argument. So, in the following case, $price is passed as the argument to doubleIt():

$price = 50;
$inflated_price = doubleIt($price);  // 100

1 Comment

What do you mean by "the parameters are what are used inside the function"?
2

parameters (if any) define the method signature. Arguments are values passed into a function.

But same difference I guess.

void function(int param1, string param2) //defines the types the function must receive.

function(1, "Hello World") 1 and "Hello World" are passed as arguments. The parameter receives (if you like) the argument.

It is explained well here

2 Comments

I thought those words meant the same thing, maybe you can clarify with an example?
@WesleyMurch Not really. Look at the link in my answer.
2

Parameters are properties of a function.

Arguments are properties of a particular call to a function.

In javascript, if you don't give a number of arguments equal to the number of parameters, the extra come across as undefined.

function f(a,b,c) // 3 parameters
f(1) // 1 argument given; inside the function f, a will be 1, and b and c will be undefined

Comments

1

13 Function Definition

Syntax

FunctionDeclaration : 
    function Identifier ( FormalParameterList (opt) ) { FunctionBody } 
FunctionExpression : 
    function Identifieropt ( FormalParameterList (opt) ) { FunctionBody } 
FormalParameterList : 
    Identifier 
    FormalParameterList , Identifier 
FunctionBody : 
    SourceElements (opt)

Officially they are called parameters, but the actual arguments are given in the same called object. However, both words are interchangeable.

Comments

0

so parameters works just like a place holder for your arguments when you going to call the function to give it a value you will give the function a value and that value stored in the place of parameter1 and parameter2

function add (parameter1 , parameter2 ) {
  return parameter1 + parameter2; 
}

add(10 (argument 1), 20 (argument2 ))

Comments

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.