JavaScript Functions

Back to JS page

Table of Contents

Functions are Code Blocks

A JavaScript function is a block of code designed to perform a particular task.

A JavaScript function is executed when "something" invokes it (calls it).

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Functions are Code Blocks</h4>
<p id="demo"></p>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Hello World!";
}
myFunction();
</script>

</body>
</html>

Example 1

Result View Example

Functions Run When You Call Them

Functions run when you call them. You can call the function by its name:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Functions Run When You Call Them</h4>
<p id="demo"></p>

<script>
function myFunction(a, b) {
  return a * b;
}
let result = myFunction(4, 3);
document.getElementById("demo").innerHTML = "4 * 3 = " + result;
</script>

</body>
</html>

Example 2

Result View Example

JavaScript Function Syntax

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().

Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).

The parentheses may include parameter names separated by commas:
(parameter1, parameter2, ...)

The code to be executed, by the function, is placed inside curly brackets: {}

function name(parameter1, parameter2, parameter3) {
  // code to be executed
}
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>JavaScript Function Syntax</h4>
<p id="demo"></p>

<script>
// Function is called, the return value will end up in x
let x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;

function myFunction(a, b) {
  // Function returns the product of a and b
  return a * b;
}
</script>

</body>
</html>

Example 3

Result View Example

A Function Can Be Used Many Times

Functions can be reused to perform the same task with different values:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>A Function Can Be Used Many Times</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>

<script>
function toCelsius(fahrenheit) {
  return (5/9) * (fahrenheit - 32);
}

document.getElementById("demo1").innerHTML = "32°F = " + toCelsius(32) + "°C";
document.getElementById("demo2").innerHTML = "68°F = " + toCelsius(68) + "°C";
document.getElementById("demo3").innerHTML = "100°F = " + toCelsius(100) + "°C";
</script>

</body>
</html>

Example 4

Result View Example

Local Variables

Variables declared within a JavaScript function become LOCAL to the function.

Local variables can only be accessed from within the function:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Local Variables</h4>
<p id="demo"></p>

<script>
function myFunction() {
  let carName = "Volvo";
  document.getElementById("demo").innerHTML = "Inside function: " + carName;
}
myFunction();
// carName is NOT accessible outside the function
</script>

</body>
</html>

Example 5

Result View Example

Functions Used as Variables

Functions can be used the same way as variables, in all types of formulas, assignments, and calculations:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Functions Used as Variables</h4>
<p id="demo"></p>

<script>
function myFunction(a, b) {
  return a * b;
}
// Function result used directly as a variable
document.getElementById("demo").innerHTML = "5 * 3 = " + myFunction(5, 3);
</script>

</body>
</html>

Example 6

Result View Example

Document

Document in project

You can Download PDF file.

Reference