Functions Flashcards
1
Q
Explain a function
A
A function is simply a piece of reusable code.
2
Q
What are the three types of functions in JS?
A
- Function declaration
- Function expression(Anonymous function)
- Arrow function(ES6)
3
Q
Write down a function declaration in JS.
A
function calcAge(currentYear, birthYear) { return currentYear - birthYear; }
3
Q
What are the differences between function declarations, function expressions & arrow functions in JS?
A
- Function declarations can be invoked before they are defined as a result of a process called hoisting.(It allows hoisting).
- A function expression is basically a function stored in a variable.
- An arrow function is a special type of function expression that is shorter, introduced in ES6. The arrow function does not have the “this” keyword.
4
Q
Write down a function declaration in JS.
A
function calcAge(currentYear, birthYear) { return currentYear - birthYear; }
5
Q
Write down a Function expression in JS?
A
const calcAge = function (currentYear, birthYear) { return currentYear - birthYear; }
6
Q
Write down an arrow function.
A
const calcAge =(currentYear, birthYear) => currentYear - birthYear;