JavaScript · JS Tutorial · Lesson 11
JS Functions
Reusable blocks of code with parameters and return values.
A JavaScript function is a block of code designed to perform a task. It is executed when "something" invokes (calls) it.
Example
<p id="demo"></p>
<script>
function multiply(a, b) {
return a * b;
}
function toCelsius(fahrenheit) {
return (5 / 9) * (fahrenheit - 32);
}
document.getElementById("demo").innerHTML =
"4 x 3 = " + multiply(4, 3) + "<br>77F = " + toCelsius(77) + "C";
</script>
- Parameters are listed inside the parentheses in the definition
- Arguments are the values received when the function is invoked
- The
returnstatement stops the function and returns a value - Functions let you reuse code: define once, use many times
Keep reading “JS Functions”
Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.