Javascript Parameters and arguments
In JavaScript, parameters and arguments refer to values passed into functions, but they serve different roles in function definition and invocation.
Parameters
Parameters are variables listed as part of a function’s definition.
They act as placeholders that allow a function to accept input when it is called.
In this example,
nameandageare parameters of thegreetfunction.
Arguments
Arguments are the actual values passed to the function when it is called (invoked).
They are the data that gets passed to the function parameters.
Here,
"Amir"and30are arguments passed to thegreetfunction. They correspond to the parametersnameandage.
Key Points
Number of Parameters and Arguments: JavaScript functions don’t enforce matching numbers of parameters and arguments. Extra arguments are ignored, while missing ones default to
undefined.Default Parameters: JavaScript allows setting default parameter values if no argument is provided for a parameter.