Create a function in JavaScript

Description

The following code shows how to create a function.

Example


<!-- w  w  w  .j  a v  a  2  s.  c om-->
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

function sum (num1, num2) {
return num1 + num2;
}
document.writeln(sum(1,2));
</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Create a function in JavaScript