[toc]

# Javascript Parameters

##	Parameters and Arguments

_Parameters_ are the named variables in a function declaration. _Arguments_ are the values passed to a function.

For example:

```js
function test(a,b) {

}
test(3,7);
```

In the above example, the __arguments__ are `3` and `7`, the values passed to the function. The __parameters__ are `a` and `b`, the variables which would be expected to be assigned the values.

Some informal discussions use the terms interchangeably, but in a discussion of parameters and arguments, you certainly need to know which is which.

##	JavaScript Functions Parameters are Flexible

Unlike most traditional languages, JavaScript has a very relaxed attitude to function parameters:

- As with the rest of JavaScript, parameters are untyped, so you can use any type of argument data you like to match the parameters, such as strings or numbers.
- JavaScript doesn’t mind if you send _fewer_ arguments than there are parameters. The remaining parameters will take on the value `undefined`.
- JavaScript doesn’t mind if you send _more_ arguments than than parameters. These additional arguments won’t be assigned to parameter variables, of course, but you can still get to them if you need to.

### The `arguments` Object

To accommodate this flexibility, a JavaScript function gets a magic variable called `arguments`, which is a numbered collection of the actual arguments.

To illustrate the above points, suppose you have a function like this:

```js
function test(a,b,c) {
	console.log(a,b,c);
	console.log(arguments.length);
	for(var i=0;i<arguments.length;i++)
		console.log(`${i}: ${arguments[i]}`);
}
test(2,5);
test(2,5,7,11);
```

If you run `test(2,5)`, you will see that there are _two_ arguments (`arguments.length`), but the _three_ parameter variables are assigned, with the last one being `undefined`. You will recall that `undefined` is not an error, but a special value.

If you run `test(2,5,7,11)`, you will see that there are _four_ arguments, but the fourth is not assigned to any variable. It is still available, however, in the `arguments` collection.

Many other languages, such as PHP throw an error if you don’t match the parameters with the right number of arguments. JavaScript just doesn’t care.

In a sense, you can think of function parameters as a convenience. You can write a function without parameters at all and then rely on the `arguments` collection to read the incoming data:

```js
function test() {
	var	a=arguments[0],
			b=arguments[1],
			c=arguments[0];
	//	etc
}
```

###	`arguments` is Not an Array

The `arguments` object is not actually an array, which means there are some things you can’t easily do with the collection. Many developers end up copying the `arguments` collection to a real array using something like this:

```js
var args=Array.from(arguments);					//	Modern JavaScript
var args=Array.prototype.slice.call(arguments);	//	Legacy JavaScript
```

On reason you would want to do this is to take advantage of `Array`’s `forEach` method:

```js
function test(a,b,c) {
	var args=Array.from(arguments);
	args.forEach((val,i)=>{console.log(`${i}: ${val}`);});
}
```

Without an array, you would iterate through the arguments like this:

```js
function test(a,b,c) {
	for(var i=0;i<arguments.length;i++)
		console.log(`${i}: ${arguments[i]}`);
}
```

###	Rest Parameters

Modern JavaScript has introduced a new syntax which allows you to capture multiple arguments into a single array. This is the so-called rest parameter:

```js
function test(...args) {
	console.log(args);
}
```

Unlike the `arugments` collection, this results in a true array, so you don’t need to copy it first:

```js
function test(...args) {
	args.forEach((val,i)=>{console.log(`${i}: ${val}`);});
}
```

You can use it in combination with ordinary parameters, as long as the rest parameter is the last one:

```js
function test(a, b, ...etc) {
	console.log(a);
	console.log(b);
	etc.forEach((val,i)=>{console.log(`${i}: ${val}`);});
}
```

If your JavaScript supports rest parameters, you probably don’t need the `arguments` collection any more.

