JavaScript | Function with default value

JavaScript | Function with default value: Here, we are going to learn how to define a function with the default values?
Submitted by Pankaj Singh, on October 12, 2018

While defining a function, by using assignment operator (=) with the argument name we can define a default value.

Example:

Here, we are defining a function named show() with two arguments id and name and the both of the arguments are defining with the default value 1) id=0 and 2) name= "no name".

Then, we are calling the function with 1) both arguments, 2) one argument and 3) no argument.

Code:

<html lang="en">
<head>
    <script>
        function show(id=0,name="no name"){
            document.write("Your name is <b>"+name+"</b> and your id is <b>"+id+"</b><br /><br />");
        }
    </script>
</head>
<body>
    <script>
        show(12,"pankaj");
        show(12);
        show();
    </script>
</body>
</html>

Output

Js code output

JavaScript Examples »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.