How To Define Function Parameters In JavaScript With Example

admin_img Posted By Bajarangi soft , Posted On 25-09-2020

In JavaScript function does not perform any checking on parameter values (arguments).today we are going to discuss how to define function parameters in java script.

How To Define Function Parameters In JavaScript With Example

Function Parameters and Arguments

Earlier in this tutorial, you learned that functions can have parameters:

function functionName(parameter1, parameter2, parameter3) {
// code to be executed
}
 


Function parameters are the names listed in the function definition.

Function arguments are the real values passed to (and received by) the function.


Parameter Rules

JavaScript function definitions do not specify data types for parameters.

JavaScript functions do not perform type checking on the passed arguments.

JavaScript functions do not check the number of arguments received.


 

Parameter Defaults

If a function is called with missing arguments (less than declared), the missing values are set to: undefined

Sometimes this is acceptable, but sometimes it is better to assign a default value to the parameter:

Example(1)

<script>
    function multiplication(x, y) {
        if (y === undefined) {
            y = 0;
        }
        return x * y;
    }
    document.getElementById("demo").innerHTML = myFunction(4);
</script>



The Arguments Object

JavaScript functions have a built-in object called the arguments object.

The argument object contains an array of the arguments used when the function was called (invoked).

This way you can simply use a function to find (for instance) the highest value in a list of numbers:

Example(2)

<script>
    function findMax() {
        var i;
        var max = -Infinity;
        for(i = 0; i < arguments.length; i++) {
            if (arguments[i] > max) {
                max = arguments[i];
            }
        }
        return max;
    }
    document.getElementById("demo").innerHTML = findMax(4, 5, 10,45);
</script>


Example(3)
 
Create a function to sum all input values:

<script>
    function sum() {
        var i;
        var sum = 0;
        for(i = 0; i < arguments.length; i++) {
            sum += arguments[i];
        }
        return sum;
    }
    document.getElementById("demo").innerHTML = sum(1, 123, 500, 115, 44, 88);
</script>


Arguments are Passed by Value

The parameters, in a function call, are the function's arguments.

JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations.


If a function changes an argument's value, it does not change the parameter's original value.


Changes to arguments are not visible (reflected) outside the function.


Complete code for function parameter in java script

<!DOCTYPE html>
<html>
<head>
    <title>Function Parameters In JavaScript</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<style>
   h2{
       color:red;
   }
</style>
<body>
<div class="container">
    <div class="text-center">
        <h1>Function Parameters In JavaScript</h1>
    </div>
    <div class="well">
        <h2>Finding Maximum Value</h2>
        <h1 id="demo1"></h1>
        <h2>Finding Sum of Numbers</h2>
        <h1 id="demo2"></h1>
    </div>
    <br>
</div>
</body>
</html>
<script>
    function findMax() {
        var i;
        var max = -Infinity;
        for(i = 0; i < arguments.length; i++) {
            if (arguments[i] > max) {
                max = arguments[i];
            }
        }
        return max;
    }
    document.getElementById("demo1").innerHTML = findMax(4, 5, 10,45);

    function sum() {
        var i;
        var sum = 0;
        for(i = 0; i < arguments.length; i++) {
            sum += arguments[i];
        }
        return sum;
    }
    document.getElementById("demo2").innerHTML = sum(1, 123, 500, 115, 44, 88);
</script>

 

Related Post