📖 Deeper dive reading: MDN Functions
In JavaScript functions are first class objects. That means that they can be assigned a name, passed as a parameter, returned as a result, and referenced from an object or array just like any other variable.
The basic syntax of a function begins with the function keyword followed by zero or more parameters and a body that may contain zero or more return statements. The return statement may return a single value. Note that there are no type declarations, as the type is always inferred by the assignment of the value to the parameter.
function hello(who) {
return 'hello ' + who;
}
console.log(hello('world'));
// OUTPUT: hello worldA function without a return value usually exists to produce some side effect like modifying a parameter or interacting with an external program. In the following example the side effect of the function is to output text to the debugger console.
function hello(who) {
who.count++;
console.log('hello ' + who.name);
}
hello({ name: 'world', count: 0 });
// OUTPUT: hello worldWhen a function is called, the caller may choose what parameters to provide. If a parameter is not provided then the value of the parameter is undefined when the function executes.
In addition to explicitly passing the value of a parameter to a function, the function can define a default value. This is done by assigning a value to the parameter in the function declaration.
function labeler(value, title = 'title') {
console.log(`${title}=${value}`);
}
labeler();
// OUTPUT: title=undefined
labeler('fish');
// OUTPUT: title=fish
labeler('fish', 'animal');
// OUTPUT: animal=fishFunctions in JavaScript are commonly assigned to a variable so that they can be passed as a parameter to some other function or stored as an object property. To easily support this common use you can define a function anonymously and assign it to a variable.
// Function that takes a function as a parameter
function doMath(operation, a, b) {
return operation(a, b);
}
// Anonymous function assigned to a variable
const add = function (a, b) {
return a + b;
};
console.log(doMath(add, 5, 3));
// OUTPUT: 8
// Anonymous function assigned to a parameter
console.log(
doMath(
function (a, b) {
return a - b;
},
5,
3
)
);
// OUTPUT: 2You can also use the abbreviated arrow syntax to write an anonymous function. The following is equivalent to the more verbose call demonstrated above. We will dive deeper into arrow functions in a later topic.
console.log(doMath((a, b) => a - b, 5, 3));Here are examples of assigning functions to variables, as well as using functions as parameters and return values.
// Anonymous declaration of the function that is later assigned to a variable
const add = function (a, b) {
return a + b;
};
// Function that logs as a side effect of its execution
function labeler(label, value) {
console.log(label + '=' + value);
}
// Function that takes a function as a parameter and then executes the function as a side effect
function addAndLabel(labeler, label, adder, a, b) {
labeler(label, adder(a, b));
}
// Passing a function to a function
addAndLabel(labeler, 'a+b', add, 1, 3);
// OUTPUT: a+b=4
// Function that returns a function
function labelMaker(label) {
return function (value) {
console.log(label + '=' + value);
};
}
// Assign a function from the return value of the function
const nameLabeler = labelMaker('name');
// Calling the returned function
nameLabeler('value');
// OUTPUT: name=valueFunctions can also be declared inside other functions. This allows you to modularize your code without always exposing private details.
function labeler(value) {
function stringLabeler(value) {
console.log('string=' + value);
}
function numberLabeler(value) {
console.log('number=' + value);
}
if (typeof value == 'string') {
stringLabeler(value);
} else if (typeof value == 'number') {
numberLabeler(value);
}
}
labeler(5);
// OUTPUT: number=5
labeler('fish');
// OUTPUT: string=fish