Please note, this is a STATIC archive of website www.tutorialrepublic.com from 10 Sep 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES
Advertisements

How to define a function in JavaScript

Topic: JavaScript / jQueryPrev|Next

Answer: Use the syntax function myFunction(){}

In JavaScript functions are defined with the function keyword, followed by the name of the function, a pair of parentheses (opening and closing), and a code block.

Let's take a look at the following example to understand how to define a function in JS.

<!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:

Advertisements
Bootstrap UI Design Templates