JavaScript Function Parameters
Parameters and Arguments
Parameters allow you to pass (send) values to a function.
Parameters are listed inside the parentheses in the function definition.
Arguments are the real values passed to, and received by the function.
Function parameters are the names listed in the function definition.
Function arguments are the real values passed to (and received by) the function.
In the example above:
- a and b are parameters
- 4 and 5 are arguments
The argument 4 is assigned to the parameter a.
The argument 5 is assigned to the parameter b.
Functions with One Parameter
A function can have one parameter.
Examples
function sayHello(name) {
return "Hello " + name;
}
let greeting = sayHello("John");
Try it Yourself »
function toCelsius(fahrenheit) {
return (5 / 9) * (fahrenheit - 32);
}
let value = toCelsius(77);
Try it Yourself »
Functions with Multiple Parameters
You can add as many parameters as you want, separated by commas.
Example
function fullName(firstName, lastName) {
return firstName + " " + lastName;
}
let name = fullName("John", "Doe");
Try it Yourself »
The Order of Arguments Matters
Arguments are assigned to parameters in the order they appear.
Example
function subtract(a, b) {
return a - b;
}
let x1 = subtract(10, 5);
let x2 = subtract(5, 10);
Try it Yourself »
The two calls above return different results because the order is different.
Incorrect Parameters
Accessing a function with incorrect parameters can return an incorrect answer:
Examples
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
let value = toCelsius("John");
Try it Yourself »
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
let value = toCelsius();
Try it Yourself »
Accessing a function without (), returns the function itself and not the result:
Example
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
let value = toCelsius;
Try it Yourself »
Note
In the examples above:
toCelsius refers to the function itself.
toCelsius() refers to the function result.
Missing Arguments
If a function is called with fewer arguments than parameters, the missing values become undefined.
Example
function multiply(a, b) {
return a * b;
}
multiply(4);
In the example above, b is undefined, so the result is NaN.
Note
A JavaScript function does not perform any checking on parameter values (arguments).
Parameter Rules
JavaScript function definitions do not specify data types for parameters.
JavaScript functions do not perform type checking on the passed arguments.
JavaScript functions do not check the number of arguments received.
Default Parameters
If a function is called with missing arguments (less than declared), the missing values are set to
undefined.
Sometimes this is acceptable, but sometimes it is better to assign a default value to the parameter:
Default Parameter Values
You can set a default value for a parameter.
This value is used if no argument is provided.
Example
function greet(name = "Guest") {
return "Hello " + name;
}
greet();
greet("Anna");
Functions Can Be Called with Variables
Arguments do not have to be values. They can also be variables.
Example
let x = 5;
let y = 6;
function multiply(a, b) {
return a * b;
}
multiply(x, y);
Common Mistakes
Confusing Parameters and Arguments
Parameters are names. Arguments are values.Forgetting the Order
Arguments are assigned to parameters by position.Missing Arguments
Use default values to avoid undefined.
Next Chapter
Next: Returning Values from Functions