Pass number to a function in JavaScript

Description

The following code shows how to pass number to a function.

Example


<!-- w  ww  .j  a  v  a 2  s  .c om-->
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function doCalcAvg(inpNum1, inpNum2, inpNum3, inpNum4)
{
var ans;
ans = (Number(inpNum1) + Number(inpNum2) + Number(inpNum3) + Number(inpNum4)) / 4;
return (ans);
}

var inpNum1 = 1;
var inpNum2 = 2;
var inpNum3 = 3;
var inpNum4 = 4;
numAvg = doCalcAvg(inpNum1, inpNum2, inpNum3, inpNum4);
document.write("The average of the four numbers you entered is: " + numAvg);
</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Pass number to a function in JavaScript