Functions
Back to Definitions
- function - a named sequence of statements that performs a specific task or useful operation
- parameter - a variable that receives an argument that is passed into a function, think of it as as the variable(s) in the function header / signature
- call/invoke/apply - to run or execute a function
- argument - a piece of data that is passed into a function when that function is called
- scope - the area of the code where a name/identifier is available for access and/or use
Defining a Function
We'll take a look at 3 ways of defining functions in JavaScript →
- function declarations
function foo(arg1, arg2) { return arg1 + arg2; }
- function expressions
const foo = function(arg1, arg2) { return arg1 + arg2; }
- arrow functions
(arg1, arg2) => { return arg1 + arg2 }
Function Declarations
Function declaration syntax:
function foo(arg1, arg2) {
return arg1 + arg2;
}
- start with the key word
function
(note that return type is not specified) - followed by function name
- followed by optional comma separated parameters within parentheses (again, no types precede the arguments)
- and finally, the function body surrounded by curly braces (with an optional return)
- what do you think you get back if return is omitted? → …
undefined
- what do you think you get back if return is omitted? → …
We'll see later that function declarations are special in that they can used before they are declared in your code!
Function Expressions
Another way to create a function is by using a function expression (remember, functions are first-class citizens): →
const doubleTheNumber = function(n) {
return n + n;
};
console.log(doubleTheNumber(5));
- declare a variable
- set it equal to the keyword,
function
- followed by parentheses and an optional list of parameters (separated by commas if more than one)
- the function body is just a block of code (surrounded by curly braces, of course! …and again with an optional return)
- note the semicolon at the end of the function definition (it is an assignment statement after all!)
- finally, the variable,
doubleTheNumber
, can be called/invoked because it's a function!
Function Expressions Continued
Function expressions, as the name implies, are expressions! →
- that means that they evaluate to a value (that is… they evaluate to a function)
- so, you can use them anywhere values are needed… some examples:
// to initialize a variable const doubleTheNumber = function(n) { return n + n; };
// as an argument const numbers = [1, 2, 3, 4]; numbers.map(function(n) { return n + n; });
// as a return value function f() { return function(n) { return n + n; }); }
Arrow Functions
Introduced in ES6, arrow functions are a way of writing function expressions in a very concise syntax.→
(arg1, arg2) => { /* body goes here */}
- it's a shorthand / more convenient way of writing a function expression
- its behavior is subtly different from regular function expressions
- it doesn't have a built in
arguments
object (we'll see this later) - its
this
is the value ofthis
where it was defined (we'll talk aboutthis
later as well)
- it doesn't have a built in
Arrow Function Syntax
There are a few ways to write arrow functions. →
- Parentheses around parameters, curly braces around body:
(p1, p2, ..., pN) => { statements }
- You can drop the curly braces if you have a single expression. The value of that expression will be implicitly returned if you drop curly braces:
(p1, p2, ..., pN) => expression // same as { return expression; }
- If there's only one parameter, you could also drop the parentheses:
singleParam => { statements }
- If you have no parameters, use empty parentheses:
() => { statements }