Javascript (Functions) Flashcards
What are functions?
A function is a reusable block of code that groups together a sequence of statements to perform a specific task.
What are function declarations?
a function declaration binds a function to a name, or an identifier. Important: A function declaration does not ask the code inside the function body to run, it just declares the existence of the function. The code inside a function body runs, or executes, only when the function is called.
Ex:
function greetWorld() {
console.log(‘Hello, World!’);
}
What makes up a function?
- The function keyword.
- The name of the function, or its identifier, followed by parentheses.
- A function body, or the block of statements required to perform a specific task, enclosed in the function’s curly brackets, { }.
Ex:
function greetWorld() {
console.log(‘Hello, World!’);
}
How do you call a function?
To call a function in your code, you type the function name followed by parentheses.
We can call the same function as many times as needed.
What do parameters in a function do?
Parameters allow functions to accept input(s) and perform a task using the input(s).
Parameters can be used as placeholders for information that will be passed to the function when it is called.
Parameters are treated like variables within the function.
Ex:
function calculateArea(width, height) {console.log(width * height); }
What are arguments (functions)?
Arguments are the values that are passed to a function when it is called. Arguments can be passed to the function as values or variables.
Ex:
calculateArea(10, 6);
What are default parameters?
Default parameters allow parameters to have a predetermined value in case there is no argument passed into the function or if the argument is undefined when called.
What are return statements?
The return statement ends function execution and specifies a value to be returned to the function caller. When we do not use the return keyword when calling a function we receive an undefined valued will be returned.
What are helper functions?
Helper functions are functions that are called within other functions.
Ex:
function multiplyByNineFifths(number) {
return number * (9/5);
};
function getFahrenheit(celsius) {
return multiplyByNineFifths(celsius) + 32;
};
getFahrenheit(15); // Returns 59
What are function expressions?
A function expression is very similar to and has almost the same syntax as a function declaration. The main difference is that with function expressions the function name can be taken out to create an anonymous function (function without a name).A function expression is often stored in a variable in order to refer to it.
Ex:
cons calculateArea = function(width, height) {
const area = width * height
}
How do you declare a function expression?
- Declare a variable to make the variable’s name be the name, or identifier, of your function. (Using const is common practice)
- Assign as that variable’s value an anonymous function created by using the function keyword followed by a set of parentheses with possible parameters. Then a set of curly braces that contain the function body.
Ex:
cons calculateArea = function(width, height) {
const area = width * height
}
How do you invoke a function expression?
Use the variable name in which the function is found and parenthesis that have the arguments to be passed in the function.
Ex:
variableName(argument1, argument2)
What are arrow functions?
Arrow functions are a shorter way to write functions by using special “fat arrow” syntax () =>
Ex:
const rectangleArea = (width, height) => {
let area = width * height;
return area;
};
What is meant by the term concise body?
We call the most condensed form of the function a concise body.
What is the first technique for refactoring a function?
If a function has a single parameter, we do not need to put this parameter in parenthesis. However, we do need to use parenthesis for functions that have zero or multiple parameters.
Ex:
zero parameter:
const functionName = () => {};
one parameter:
const functionName = paramOne => {};
multiple parameters:
const functionName = (paramOne, paramTwo) => {};