How to call a function in JavaScript?


JavaScript allows us to write our own functions as well. To invoke a function somewhere later in the script, you would simply need to write the name of that function.

Example

You can try to run the following code to learn how to call a function in JavaScript −

<html>
   <head>
      <script>
         function sayHello() {
            document.write ("Hello there!");
         }
      </script>
   </head>

   <body>
      <p>Click the following button to call the function</p>
      <form>
         <input type = "button" onclick = "sayHello()" value = "Say Hello">
      </form>
      <p>Use different text in write method and then try...</p>
   </body>
</html>

Updated on: 19-Jun-2020

768 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements