Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between arguments and parameters in javascript?

Tags:

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?

like image 595
RufioLJ Avatar asked Oct 13 '12 15:10

RufioLJ


People also ask

What is the difference between an argument and a parameter?

The values that are declared within a function when the function is called are known as an argument. Whereas, the variables that are defined when the function is declared are known as a parameter.

What is an argument in JavaScript?

The arguments is an object which is local to a function. You can think of it as a local variable that is available with all functions by default except arrow functions in JavaScript. This object (arguments) is used to access the parameter passed to a function. It is only available within a function.


2 Answers

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
like image 69
David G Avatar answered Sep 18 '22 18:09

David G


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
like image 20
NullPoiиteя Avatar answered Sep 20 '22 18:09

NullPoiиteя