published: 13th of May 2020
// Basic function that accepts a parameter
function functionName(param) {
// Do stuff with param
// optionally return something
}
// Typescript version with type hints
function functionName(param: string) {
// Do stuff with param
// optionally return something
}
// Parameters can have default values
function functionName(param1, param2="blah") {
// Do stuff with params
// optionally optionally return something
}
// Typescript version with default values
function functionName(param1: string, param2: string="blah") {
// Do stuff with params
// optionally return something
}
// Call a function
stuff("things");
Anonymous functions are just functions without a name. They can optionally be assigned to a variable.
function(param) {
// Do stuff with param;
// optionally return something
}
const functionName = function(param) {
// Do stuff with param;
// optionally return something
}
Arrow functions are a shorthand syntax for anonymous functions.
(param) => {
// Do stuff with param;
// optionally return something
}
// Assign arrow function to variable
const functionName = (param) => {
// Do stuff with param;
// optionally return something
}
// If an Arrow function has one statement and returns a value
// it can be written on a single line.
const functionName = (param) => `Say: ${param}`
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
https://www.w3schools.com/Js/js_arrow_function.asp
https://www.logicbig.com/tutorials/misc/typescript/function-optional-and-default-params.html