Functions JS Flashcards

1
Q

What is a function declaration vs function expression? What are the differences? (hoisting)

A
function declaration === function doStuff() {};
function expression === const doStuff = function() {}
Or
const doStuff = () => {
}

Function declarations are hoisted, expressions are not. Hoisting allows functions to be safely used in code before they are declared. e.g. it hoists all declarations to the top of their scope by default, so it doesn’t matter where you declare and use them.

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

What are anonymous functions, give example.

A

Functions without a name. Arrow functions are shorthand for declaring anonymous functions.

example:
let sayHello = (name) => {
console.log(name)
}
sayHello("Chris")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain scope.

  • Block scope
  • Function scope
  • Global scope
A
Global = Variables declared outside of any function.
Local = A variable declared inside a function is only visible inside that function
Block = A let or Const declared inside a block e.g. an if Statement

Variables declared with the var keyword can NOT have block scope.

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

Parameters vs Arguments

A
Parameters are the items listed between the parentheses in the function declaration. 
let b = (paramOne, paramTwo) => {
}

Function arguments are the actual values we decide to pass to the function
b(argOne, argTwo)

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

What is a Higher-Order function? Some Examples.

A

A higher order function is identified by:

  • A function that returns a function
  • A function that takes in a function as an argument
Examples:
forEach()
map()
filter()
reduce()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a callback function?

A

A callback is a function passed into another function as an argument to be executed later.

students.map(callbackfunction(item){})

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

When do you need to use function declaration over a function expression?

A
function declarations have access to the "this" keyword
function name() {
   //has access to "this"
}
let name = () => {
//does NOT have access to "this"
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a Set, and name a use case.

A

Sets are basically arrays but every value has to be unique.

Sets are good for removing duplicate values from an array.

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

What is a Map, and name a use case.

A

Maps are objects that remember the insertion order, and can have any value (OBJECTS OR PRIMITIVE VALUES) as keys.
Maps are good if you need to use non string values (e.g. objects) as keys

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