Create a function variable in JavaScript

Description

The following code shows how to create a function variable.

Example


<!--from w  w  w .  j av a2  s  . c om-->
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var sum = function(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 variable in JavaScript