Function Expressions Flashcards

1
Q

What types of functions are ‘hoisted’?

A

Only function declarations, not function expressions or arrow functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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);
});

A

an anonymous function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a function expression?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can we define a named arrow function?

A

Arrow functions are always anonymous: there’s no way to define a named arrow function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how can you hame a function expression?

A

let squaredNums = [1, 2, 3].map(function squareNum(num) {
return num * num;
}); // => [1, 4, 9]

let foo = function bar() {};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why would we name a function expression?

A

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.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can we access a function expression by its name?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly