Function Expressions Flashcards
What types of functions are ‘hoisted’?
Only function declarations, not function expressions or arrow functions
What do we call a function like this?
let prompt = function() { // Assign to a variable
};
or
[1, 2, 3].forEach(function(elem) { // pass to another function
console.log(elem);
});
an anonymous function
What is a function expression?
The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions. When function creation happens in the context of the assignment expression (to the right side of =), this is a Function Expression.
Can we define a named arrow function?
Arrow functions are always anonymous: there’s no way to define a named arrow function
how can you hame a function expression?
let squaredNums = [1, 2, 3].map(function squareNum(num) {
return num * num;
}); // => [1, 4, 9]
let foo = function bar() {};
Why would we name a function expression?
If the function has a name, the stack trace uses that name to help you determine where the error occurred. Without the name, JavaScript merely reports the location as “anonymous.”
Can we access a function expression by its name?
The function name given to a function expression isnot visiblein the scope that includes the function expression. ie
let foo = function bar() {};
foo(); // This works
bar(); // This does not work