Javascript Function Parameters are the names that are defined in the function definition and real values passed to the function in the function definition are known as arguments.
Syntax:
function Name(paramet1, paramet2, paramet3,...) {
// Statements
}
Parameter Rules:
- There is no need to specify the data type for parameters in JavaScript function definitions.
- It does not perform type-checking based on the passed-in JavaScript functions.
- It does not check the number of received arguments.
Parameters:
- Name: It is used to specify the name of the function.
- Arguments: It is provided in the argument field of the function.
These are the types of parameters that can be used in JavaScript:
Example: This example uses JavaScript function parameters and finds the largest number.
Javascript
function GFG(var1, var2, var3) {
if (var1 > var2) {
if (var1 > var3) {
return var1;
}
else {
return var3;
}
}
else {
if (var2 > var3) {
return var2;
}
else {
return var3;
}
}
}
console.log(GFG(4, 50, 6));
|
Defaults Parameter
The default parameters are used to initialize the named parameters with default values in case, when no value or undefined is passed.
Syntax:
function Name(paramet1 = value1, paramet2 = value2 .. .) {
// statements
}
Example: This example uses default parameters and performs the multiplication of numbers.
Javascript
function GFG(num1, num2 = 2) {
return num1 * num2;
}
console.log(GFG(4));
|
Arguments Object
The arguments objects are inbuilt objects in JavaScript functions. In all non-arrow functions, the arguments object is a local variable. Analyze the arguments inside the function by using its arguments object.
Example: This example uses argument objects as parameters and finds the largest of numbers.
Javascript
function GFG() {
let i;
let maxnum = -Infinity;
for (i = 0; i < arguments.length; i++) {
if (arguments[i] > maxnum) {
maxnum = arguments[i];
}
}
return maxnum;
}
console.log(GFG(10, 12, 500, 5, 440, 45));
|
Output:
.png)
Arguments Pass by Value
In a function call, the parameters are called as arguments. The pass-by value sends the value of the variable to the function. It does not send the address of the variable. If the function changes the value of arguments then it does not affect the original value.
Example: This example demonstrates the above-used approach.
Javascript
function GeeksForGeeks(var1, var2) {
console.log("Inside the GeeksForGeeks function");
var1 = 100;
var2 = 200;
console.log("var1 =" + var1 + " var2 =" + var2);
}
var1 = 10;
var2 = 20;
console.log("Before function calling");
console.log("var1 =" + var1 + " var2 =" + var2);
GeeksForGeeks(var1, var2);
console.log("After function calling");
console.log("var1 =" + var1 + " var2 =" + var2);
|
Output
Before function calling
var1 =10 var2 =20
Inside the GeeksForGeeks function
var1 =100 var2 =200
After function calling
var1 =10 var2 =20
Objects passed by Reference
In Objects Pass by Reference, passing the address of the variable instead of the value as the argument to call the Function. If we change the value of the variable inside the function then it will affect outside function variables.
Example: This example demonstrates the above-used approach.
Javascript
function GeeksForGeeks(varObj) {
console.log("Inside GeeksForGeeks function");
varObj.a = 100;
console.log(varObj.a);
}
varObj = { a: 1 };
console.log("Before function calling");
console.log(varObj.a);
GeeksForGeeks(varObj)
console.log("After function calling");
console.log(varObj.a);
|
Output
Before function calling
1
Inside GeeksForGeeks function
100
After function calling
100
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
15 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...