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 Property Marvels - A Leading Real Estate Portal for Premium Properties