python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
How can I declare optional parameters in JavaScript function?

How can I declare optional parameters in JavaScript function?

The live example link http://embed.plnkr.co/VgRD8B/

//DECLARE OPTIONAL PARAMETERS IN BELOW FUNCTION
function myFunction(i, j) {
    i = i || 0;
    j = j || 0;

   //IN THE ABOVE LINE, (i/j) WILL BE SET EITHER TO VALUE OF (i/j) OR TO DEFAULT VALUE (0).
}

For example in detail

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script>
        //DECLARE OPTIONAL PARAMETERS IN BELOW FUNCTION
        function myFunction(i, j) {
            i = i || 0;
            j = j || 0;

           alert('i : ' + i + ' j : ' + j);
           //IN THE ABOVE LINE, (i/j) WILL BE SET EITHER TO VALUE OF (i/j) OR TO DEFAULT VALUE (0).
        }

        //Calling to function.
        myFunction(2);
    </script>
</head>
<body>
    <h1>DECLARE OPTIONAL PARAMETERS FUNCTION</h1>
    <p>
        For more detail go to link
        <a href="http://www.code-sample.com/">example</a>
    </p>
</body>
</html>





ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions