Read: 06 - Programming with JavaScript:

JavaScript Functions

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).

JS Functions

Function Invocation

The code inside the function will execute when “something” invokes (calls) the function:

Function Return

When JavaScript reaches a return statement, the function will stop executing.

Return

The () Operator Invokes the Function

Using the example above, toCelsius refers to the function object, and toCelsius() refers to the function result.

Accessing a function without () will return the function object instead of the function result.

Example:

function toCelsius(fahrenheit) {

return (5/9) * (fahrenheit-32);

} document.getElementById(“demo”).innerHTML = toCelsius;

Local Variables

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

Local variables can only be accessed from within the function.

Logic of functions:

Why functions?