Parameters and Arguments in JavaScript
Parameters and Arguments
78
In JavaScript, functions can accept input values called parameters when they are defined. Parameters are defined inside the parentheses after the function name, and they act as placeholders to receive data when the function is called.
For example:
function calculateArea(width, height) { return width * height; }
In the above example, the calculateArea()
function has two parameters, width
and height
. When we call the function and pass values for width
and height
as arguments, the function will use those values to perform some actions and return a result.
let area = calculateArea(5, 7); //area will be 35
In this example, we are calling the calculateArea()
function and passing the values 5 and 7 as arguments. These values are then received by the parameters width
and height
inside the function, and the function performs the multiplication operation to calculate the area, which is returned and assigned to the area
variable.
In essence, parameters are like placeholders that tell a function what values to expect as input, while arguments are the actual values passed to the function when it is called.
March 25, 2023