WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

How to define a function in JavaScript

Topic: JavaScript / jQueryPrev|Next

Answer: Use the syntax function myFunction(){}

The following example will show how to define a function in JavaScript.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="utf-8">
  • <title>Define a Function in JavaScript</title>
  • </head>
  • <body>
  •     <script>
  •         function myFunction(){ 
  •             alert("You have successfully defined the function!"); 
  •         }
  •     </script> 
  •     <button type="button" onclick="myFunction();">Click Me</button>
  • </body>
  • </html>

Related FAQ

Here are some more FAQ related to this topic:

Your Feedback:

We would love to hear from you, please drop us a line.