JavaScript Function Arguments
Posted by Superadmin on May 03 2023 02:46:01

JavaScript Function Arguments

JavaScript Function AXrguments

Introduction to JavaScript Function Arguments

JavaScript arguments can be passed through functions within the function keyword. Arguments can be given with any name, but it should meet the JavaScript naming convention per a better understanding of the code. We must be aware of the difference between arguments and parameters for a better understanding of the concept without any confusion. Parameters defined as while we are declaring a function, there some names within the function is known as Parameters.

Example:

function add(a, b, c){
return a+b+c;
}

Explanation: Here, a, b, c are called as parameters.

Arguments: While we are calling a function, passing some values are called as Arguments.

Example: 

add(2,4,6);

Explanation: Here, calling above declared add function with 2,4,6 values, 2,4,6 are called arguments. In one word, Arguments are real values.

How does Arguments Work in JavaScript?

Syntax:

function addition(a,b,c) //function with parameters
{
return a+b+c;
}
addition(1,2,3);//function calling with arguments

JavaScript Output Methods

Console.log: It will display the out in the browser console.

Javascript Function Arguments output 1

See the above image highlighted in the console with a blue mark.

document.write: It displays out in the browser directly as like html pages.

Javascript Function Arguments output 2

Examples with Code: 

It is divided into 4 types

1. Function with no parameters and Function calling with no arguments

Both function definition and calling function do not have parameters and arguments, respectively.

Syntax:

function functionName(){ //function definition
//code
}
functionName();//function calling

Example:

ReturnName.js

<!DOCTYPE html>
<html>
<body>
<font color="green"><p align="center">Function with no parameters and Function calling with no arguments</p></font>
<script>
function getMyName() {
return "I am Paramesh, came from with return statement"
}
document.write(getMyName());
</script>
</body>
</html>

Output:

Javascript Function Arguments output 3

Note: In JavaScript, we can use the return statement even does have not to return type because JavaScript Engine automatically detects the resultant return type. Thus, conclude JavaScript does not have any check return type.

NonReturnString.js

<!DOCTYPE html>
<html>
<body>
<font color="green"><p align="center">Function with no parameters and Function calling with no arguments</p></font>
<script>
function getOutput() {
document.write("Yup, I am without return statement output")
}
getOutput();
</script>
</body>
</html>

Output:

Javascript Function Arguments output 4

2. Function with no parameters and Function calling with arguments

The function definition does not have any parameters, and function calls have some parameters.

Syntax:

function functionName(){
//code
}
functionName(x,y,z);

Example:

IndexValue.js

<!DOCTYPE html>
<html>
<body>
<font color="green"><p align="center">Function with no parameters and Function calling with arguments</p></font>
<script>
function getIndexValue() {
document.write("Index values is : "+arguments[0]);
document.write("<br>"); //make output in a new line
document.write("Index values is : "+arguments[1]);
document.write("<br>");
document.write("Index values is : "+arguments[2]);
}
getIndexValue(4,5);
</script>
</body>
</html>

Output:

Javascript Function Arguments output 5

Explanation:

3. Function with parameters and Function calling with no arguments:

A function definition has parameters, and function calling does not have any arguments.

Example:

ReturnSum.js

<!DOCTYPE html>
<html>
<body>
<font color="green"><p align="center">Function with parameters and Function calling with no arguments</p></font>
<script>
function getSum(a=1,b=2,c=3) {
return "The Sum is: "+(a+b+c);
}
document.write(getSum());
</script>
</body>
</html>

Output:

output 6

Explanation:

StringPlusValueString.js

<!DOCTYPE html>
<html>
<body>
<font color="green"><p align="center">Function with parameters and Function calling with no arguments</p></font>
<script>
function getSum(a=1,b=2,c=3) {
document.write("The Sum is: "+a+b+c);
}
getSum();
</script>
</body>
</html>

Output:

output 7

Explanation:

In any programming language

String+String is always String only. In the above example, we are adding a+b+c to the string “The Sum is: “. If we write “The Sum is: “+a+b+c, then the output would become

The Sum is: 123

Is it surprised you? Well, it is because “The Sum is:” are a string and value always string only.

We are keeping the addition of 3 numbers in bracket((a+b+c)), then we add it to the string because any programming language follows the BODMAS rule. First executes Bracket followed by Of, Division, Multiplication, Addition, and Subtraction.

4. Function with parameters and Function calling with arguments

Both function definition and function calling have parameters and arguments, respectively.

Syntax:

function functionName(a,b)
{
//code
}
functionName(2,1);

Example: 

MuliplicationTable.js

<!DOCTYPE html>
<html>
<body>
<font color="green"><p align="center">Function with parameters and Function calling with arguments</p></font>
<script>
function getMultiples(nThTable,upTo) {
document.write("The 10 Multiples : ");
document.write("<br>")
for(let i=1;i<=upTo;i++)
{
document.write(nThTable+"*"+i+" = "+nThTable*i);
document.write("<br>")
}
}
getMultiples(10,10);
</script>
</body>
</html>

Output:

output 8

Explanation: 

Suppose we want to pass arguments from the calling function and parameters from the function definition. JavaScript Engine does not complain about it. Thus, JavaScript allows Function with parameters and Function calling with arguments.

Conclusion

Function arguments can be passed to parametrized and non-parametrized functions without any errors from JavaScript Engine.