Press "Enter" to skip to content

Functions Examples in JavaScript

Definition :

    Function is a block of code which is meant for performing the operations on invoking.

  • with argument no return value :

this is the function which will take arguments but won’t return any values then it is called as with argument and no return value function.

Program :

<html>

     <head>

                     <script language=”javascript”>

                                     function display()

                                                 {

                                                   a=parseFloat(document.getElementById(“t1”).value);

                                                   b=parseFloat(document.getElementById(“t2”).value);

                                                   if(op==”Add”)

                                                   c=a+b;

                                                   else

                                                   c=a-b;

                                                   document.write(“Result is “+c);

                                                  }

                                </script>

                </head>

                <body>

                     <input type=”text” id=”t1″/> <br>

                                 <input type=”text” id=”t2″/> <br>

                                 <input type=”button” value=”Add” onclick=”display(this.value)”> <br>

                                 <input type=”button” value=”sub” onclick=”display(this.value)”> <br>

                </body>

</html>

  • no argument with return value function :

it is a function which won’t take any arguments from the function call but returns the values to the function call is called as function with no argument with return value function.

Program :

<html>

     <head>

                     <script language=”javascript”>

                                     function display()

                                                 {

                                                   a=document.getElementById(“t1”).value;

                                                   b=document.getElementById(“t2”).value;

                                                    if(a==”admin” && b==”admin”)

                                                                return true;

                                                                else

                                                                {

                                                                  alert(“invalid input”);

                                                                  return false;

                                                                }

                                                 }

                                </script>

                </head>

                <body>

                    <form action=”img.jpg” onsubmit=”return display()”/>

                     <input type=”text” id=”t1″/> <br>

                                 <input type=”password” id=”t2″/> <br>

                                 <input type=”submit” value=”validate”/>

                </body>

</html>

Self Invoking Function :

   It is a function which gets invoke on its own then it is called as self invoking function. Which is meant for performing the operations automatically whenever the web pages loads.

Program :

<html>

     <head>

                    <script>

                                    (function() {

                                                  alert(“HELLO WELCOME”);

                                                  }) ();

                                </script>

                </head>

                <body>

                </body>

</html>

Constructor function :

This is a function which holds the parameters and the logic in a single statement and gets executed during object creation only. It will take assigned variable name as function name for performing the operations.

Program :

<html>

     <head>

                    <script language=”javascript”>

                                    var addition=new Function(“a”,”b”,”return a+b”);

                                                alert(addition(10,20));

                                </script>

                </head>

                <body>

                </body>

</html>

Closure function :

  It is an inner function which gets executed on invoking the external function.

Program :

<html>

    <head>

                     <script language=”javascript”>

                                     var add=(function() {

                                                 var c=0;

                                                 return function()

                                                 {

                                                   return c++;

                                                 }

                                                }) ();

                                                document.write(add()+”</br>”);

                                                document.write(add());

                                </script>

                </head>

                <body>

                </body>

</html>

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *