A function is a piece of code that sits dormant until it is referenced or called upon to do its "function". In addition to controllable execution, functions are also a great time saver for doing repetitive tasks.
To create a function, type the function keyword followed by a name for the function, followed by an opening, then a closing, parentheses, followed by an opening curly bracket “{“ and ending with a closing curly bracket “}”.
Here is the syntax for creating a function:
function name(){}Example Function in JavaScript
HTML & JavaScript Code:
<html> <head><script type="text/javascript">
<!-- function popup()
{
alert("Hello World") } //-->
</script>
</head>
<body>
<input type="button" onclick="popup()" value="popup">
</body>
</html>