/

HTML / CSS / JavaScript Tutorial

JavaScript Statement: function

[this page | pdf | back links]

In JavaScript, the function statement declares a function (akin to a subroutine or procedure in some other programming languages), i.e. a set of statements that can be executed (which can return a result) from elsewhere within the code.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting values</th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
function multiply(a, b) {
  var c = a * b;
  return c;
}

const arrowMultiply = (a, b) => { return a * b; }
const arrowMultiplySingleLine = (a, b) => a * b;
const arrowMultiplyBy2 = a => a * 2;

var x = multiply(3, 4);
var y = arrowMultiply(3, 4);
var z = arrowMultiplySingleLine(3, 4);
var q = arrowMultiplyBy2(3);

document.getElementById("Example").innerHTML =
  'function multiply(a,b) {<br>' +
  '&nbsp;&nbsp;var c = a * b;<br>' +
  '&nbsp;&nbsp;return c;<br>' +
  '}<br><br>' +
  'const arrowMultiply = (a, b) => { return a * b; }<br>' +
  'const arrowMultiplySingleLine = (a, b) => a * b;<br>' +
  'const arrowMultiplyBy2 = a * 2;<br><br>' +
  'var x = multiply(3,4);<br>' +
  'var y = arrowMultiply(3,4);<br>' +
  'var z = arrowMultiplySingleLine(3,4);<br>' +
  'var q = arrowMultiplyBy2(3);<br>';

document.getElementById("Result").innerHTML = "x = " +
   x + "<br>y = " + y + "<br>z = " + z + "<br>q = " + q;
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Reserved Words


Desktop view | Switch to Mobile