Example

Function parameters & arguments

HTML

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <link name="favicon" type="image/x-icon" href="https://skillzam.com/uploads/system/e353194f3b7cd1b75d69c3ab88041985.png" rel="shortcut icon" />
    <title>Function parameters & arguments</title>
</head>    
<body>
<!--
    Function parameters : 
    They are names listed in the function definition/declaration.
    Parameter Rules 
        - Function definitions do not specify data types for parameters.
        - Functions do not perform type checking on the passed arguments.
        - Functions do not check the number of arguments received.
    
    Function arguments :
    They are real values passed to (and received by) the function.
    Functions have a built-in object called the arguments object.
    Argument object contains an array of arguments used when function was called (invoked).
    Changes to arguments are not visible (reflected) outside the function.
    Changes to object properties are visible (reflected) outside the function.
-->

    <h1>Demo: JavaScript Functions</h1>

    <div>
        <h2>Function parameters & arguments</h2>
        <p id="para1"></p>
    </div>


    <!-- JavaScript code Starts here -->
    <script>

        /************************************************/
        // Function parameters & arguments
        // Example : Largest of two distinct numbers
        //
        function largestValue(num1, num2) {   // function declaration with 2 parameters
            if (num1 > num2) {
                return(`Largest number is ${num1}`);  // function return statement
            } else {
                return(`Largest number is ${num2}`);  // function return statement
            }
        }
        let number1 = 22;
        let number2 = 33;
        let funcRtn = largestValue(number1, number2); // function invoking with 2 arguments
        document.getElementById('para1').innerHTML = funcRtn;

    </script>   
    <!-- JavaScript code Ends here -->

</body>
</html>
                    
                                               

Output

View original