How is a function declared and called?
function keyword > function identifier () {
console.log (‘Water the plants.’);
}
function functionname () {
console.log (‘Water the plants.’);
}
it’s called using the below.
functionname();
What are function parameters and how are they declared?
Function parameters are basically variables.
The parameters are defined in the parenthesis when declaring the functions.
What is an argument?
Values passed to a function when said function is called.
Example
rectangleArea(5,10);
you can pass an argument as a value or create a variable with a value and then pass the variable as an argument.
How is a default parameter value created and then an argument value passed to overwrite the default value.
Assign the parameter a value in the parenthesis to create a default value.
When calling the function provide a value to overwrite the default.
function greeting (name = ‘stranger’) {
console.log(Hello, ${name}!)
}
greeting (Hassam);
What is the purpose of the return keyword with in a function?
To pass back information from the function, without the keyword JavaScript will calculate but not store the value.
Without the return keyword any value called outside the function will have a value of undefined.
Does using the return keyword stop the execution of code?
Yes any code after the return keyword within a functions body will stop all code below from running.
What is a helper function and it’s benefits?
A function within a function, the purpose is for one function to call another to get a return value.
Makes the code easier to read and debug. Breaking down larger difficult code into smaller manageable chunks.
How do you assign a function value to a variable.
function costOfMonitors(rows, columns) {
return monitorCount(rows, columns) * 200;
}
const totalCost = costOfMonitors(5, 4);
Function Declaration vs Function Expression?
Declaration - A declared function that requires a name.
Expression - Often created with a variable and doesn’t need name, called a anonymous function.
How is a function expression written?
const sizeConversion = function(width, height) {
const area = width * height;
return area;
};
How is a function expression written using a fat arrow notation (Arrow function)?
const sizeConversion = (width, height) => { const area = width * height;
return area;
};
Writing an function expression without the function keyword.
The most condensed form of the function is known as a?
Concise body, refactored using arrow functions.
How are parameters written when using arrow functions?
Zero parameters requires parenthesis ().
One parameters require no parenthesis.
More than one parameter requires parenthesis().
What is an Implicit Return?
To return a function value the return keyword isn’t needed when when using a function expression alongside arrow notation. The function also needs to be written in a single line.
SINGLE-LINE-BLOCK
const sumNumbers = number => number + number;
MULTI-LINE-BLOCK
const sumNumbers = number => {
const sum = number + number;
return sum; «RETURN STATEMENT
}
Why would a function pass undefined when logged to the console?
A function without a return will pass back undefined.