Functions Flashcards
Arrow Functions (ES6)
Does not require the ‘function’ keyword, and uses a fat arrow (=>) to separate the parameters from the body
Arrow functions with a single parameter do not need () around the parameter list
const checkAge = age => { //do something}
Arrow functions with a single expression do not require the use of the return keyword
const multiple = (a, b) => a*b;
Anonymous Functions
Functions that do not have a name property
const rocketToMars = function() {
return ‘BOOM!’;
}
Function Expression
Create functions inside an expression instead of as a function declaration. They can be anonymous and/or assigned to a value
const dog = function() {
return ‘Woof!’;
}
const dog = () => ‘Woof!’;
Parameters
Used as variables inside the function body
When a function is called, these parameters will have the value of whatever is passed in as arguments
function sayHello(name) {
return Hello, ${name}!
;
}
return
Used to return (pass back) values from functions
If a return value is not specified, the function will return undefined by default
Calling functions
Functions can be called or executed using parentheses following the function name
// Defining the function
function sum(num1, num2) {
return num1 + num2;
}
// Calling the function
sum(2, 4); // 6
Global Scope
A value/function in the global scope can be used anywhere in the entire program
Variables declared outside of blocks or functions exist in the global scope
File/Module Scope
A value/function in the file/module scope can only be accessed within that file
Block Scope
A value/function is only visible/accessible within a code block { … }
const and let are block-scoped variables