JavaScript - Function Parameters
JavaScript - Function Parameters
Function Parameters and Arguments
Thefunction parametersin JavaScript are variables listed inside the parentheses in the function definition. A function can have multiple parameters separated by commas. Thefunction argumentsare the values that are passed to function when it is called. We define function listing the parameters and call the function passing the arguments.
The number of arguments that you pass to the function must match the number of parameters in the function definition. If not, you may get unexpected result.
Syntax
The syntax to usefunction parametersin JavaScript is as follows β
function functionName (parameter1, parameter2, parameter3) {
//statements
}In the above syntax, the function parameters are βparameter1β, βparameter2β, and βparameter3β.
Parameter Rules
- JavaScript functions donβt check the number of arguments passed while invoking the function.
- Donβt need to specify the data type of function parameters.
- JavaScript compiler doesnβt perform the type-checking on the passed function arguments.
The JavaScriptfunction argumentsare the variables or values passed to the function while invoking it.
functionName (10, b, 'Hello');In the above syntax, the first argument is of number data type, and the third is of string data type. The second argument is variable, which is defined earlier in the code.
Example: Function Parameters and Arguments
In the below code, the mult() function takes 4 parameters. You can observe that the type of parameters is not defined. We multiply the parameter values in the function body and return the resultant value.
While calling the function, we passed 4 number values as a function argument. Users can observe the output of the function for the different function arguments.
Output
The multiplication of 2, 3, and 4 is 24Argument Object
In JavaScript, each function can have an βargumentsβ object. It contains all passed arguments while invoking the function in the array format. We can traverse through the array and get each argument even if the functionβs parameters are not specified.
Example
In the example below, the function definition doesnβt contain any parameters, but we have passed the 4 arguments while calling the function. So, we traverse through the arguments[] array using the loop inside the function body to access all arguments one by one.
In the function body, we merge all arguments and return the βfinalβ string.
Output
The merged string is: Hi I am John!Passing Arguments by Value
In the function, when you pass the argument by value to a function call, it sends the argument value to the parameter of the function definition. So, when you update the function parameters, the function argument doesnβt get changed.
Example
In the below code, we have defined the βval1β and βval2β variables outside the function and passed them as a function argument.
In the function body, we change the parameter value. In the output, you can see that even after updating the parameter value, the actual value of the βval1β and βval2β is not changed.
Output
Before calling the function!
val1 = Hello, val2 = World
After calling the function!
val1 = Hello, val2 = WorldPassing Arguments by Reference
When you pass the object as an argument, the function sends the address of the object as a parameter to the function definition. So, it is called the arguments are passed by reference.
In the function body, if you change the property of an object, it will also reflect the outside of the function.
Example
In the below code, we pass the βobjβ object as a function argument. In the function body, we change the value of the βdomainβ property of the object.
In the output, you can observe that the value of the βdomainβ property is changed even outside the function after invoking the function as objects are passed by reference.
Output
Before calling the function!
domain = www.google.com
After calling the function!
domain = www.codescompiler.com