JavaScript Function Literals

The Function Literals

JavaScript 1.2 introduces the concept of function literals which is another new way of defining functions. A function literal is an expression that defines an unnamed function. The syntax for a function literal is much like a function statement, except that it is used as an expression rather than a statement and no function name is required.

  •       
                        <script type = "text/javascript">
    var variablename = function(Argument List) {
            Function Body 
        };
    </script>
                        
  •       
                        <html>
    <head>
    <script type = "text/javascript">
        var func = function(x,y) { 
        return x*y 
        };
        function secondFunction() {
        var result;
        result = func(10,20);
        document.write ( result );
        }
    </script>
    
    </head>
    
    <body>
    <p>Click the following button to call the function</p>
    
    <form>
    <input type = "button" onclick = "secondFunction()" value = "Call Function">
    </form>
    
    <p>Use different parameters inside the function and then try...</p>
    </body>
    </html>
    
                        

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1