Introduction to JavaScript Functions

What are functions?

Functions are a named list of statements and declarations with pre-defined input parameters and output values. Functions, in JavaScript, are just regular values and we can bind a constant or variable to a function object.

We already know that JavaScript is dynamically typed and hence does not require us to specify the types of a function's input parameters or output values.

function f2c(f) {
return (f - 32) * 5 / 9
}

The example above defines a function f2c that converts Fahrenheit temperatures to Celsius. The function signature (first line) tells us the name of the function and the input parameters it accepts. In this case the name is f2c and it takes a single parameter f.

const hundred = f2c(100)
const fifty = f2c(50)

We can then use the defined function f2c, invoke (call and execute) with our Fahrenheit values and get Celsius degrees in return. Notice how when we later use the f2c function, we don't have to worry about getting the mathematical conversion formula right. This is the major selling point of splitting logic into small task specific functions.

If we ever decide to change the conversion implementation, we only need to make changes in one place: the f2c function.