Monday, 21 January 2013

Javascript parameter is optional

In Javascript, the parameter in the function is optional. If the caller did not pass in the parameter value (as shown in test1 function), then, the value of "a" is "undefined". So, each time before you are using the incoming parameter value, make sure that you check it before using it.


You may copy the following tags into any html file and then debug it in Visual Studio.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        function check(a) {
            var i = 0;
            if (a) {
                i = a;
            }
            else {
                i = -1;
            }
        }
        function test1() {
            check();
        }
        function test2() {
            check(12345);
        }
        function test3() {
            check(null);
        }
    </script>
</head>
<body>
    <input type="button" name="name" value="test1" onclick="return test1();" />
    <input type="button" name="name" value="test2" onclick="return test2();" />
    <input type="button" name="name" value="test3" onclick="return test3();" />
</body>
</html>

No comments:

Post a Comment

We are moving

We are moving this blog to our new blog site: https://ciysys.com/blog/nodejs.htm