Home » JavaScript

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.