JavaScript – Functions

Function:

  • Function performs any task using pre-defined set of instructions.
  • Functions must be defined with keyword called ‘function’
Syntax:Example:
      function identity()
      {
             …..
             Logic
             …..
      }
       function add()
       {
            var a=10, b=20;
            var c = a+b;
            print(c);
       }

Note: We always perform operations when action performed. For example, display alert message when user clicks on button

<html>
            <head>
                        <script>
                                    function clickMe()
                                    {
                                                alert(“Hello”);
                                    }
                        </script>
            </head>
            <body>
                        <h2> JavaScript Alert Box </h2>
                        <button onclick=”clickMe()”>Click Me</button>
            </body>
</html>

document.write():

  • “document” is a pre-defined object in JavaScript.
  • “document” represents “webpage”.
  • write() is a pre-defined method to display information.
  • If we invoke write() method on document object then it writes the information on webpage

Scroll to Top