Functions Flashcards

1
Q

What is a general definition of a function?

A

A function is a block of code that executes tasks in a specific order.

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

How do you declare a function in JavaScript

A

function [functionName] (parameters) {

[workingCode]

}

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

What are parameters of a function?

A

Parameters are optional, comma separated variables that you wish to declare for the function.

These parameters can be assigned values when you use the function.

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

How can you use a function?

A

You can use a function by calling (evoking, executing) it with its name and the brackets that surround any parameters (even if there are no parameters).

functionName(parameters)

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

When do parameters become arguments in functions execution.

A

Parameters become arguments when we give actual values during a function execution.

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

What is the difference between parameters and arguments when using functions?

A

Parameters - variables used when declaring a function.

Arguments - values used in place of parameters when invoking a function.

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

What is the result of defining a parameter in a function but not passing in an argument during execution

A

The result is that the parameter value will be ‘undefined’.

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

What is returned from a function if a return statement is not included in the code block?

A

Undefined is returned

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

What types of values can JavaScript pass around?

A

JavaScript can pass Primitives and Objects.

Expressions need to be evaluated to either of these two values before being passed.

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

What happens if you pass a function with parameters as an argument to another function?

A

The function argument needs to be evaluated before being used in the calling function (in this case it is an expression)

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

What is the term for a function (without parameters) that is passed as an argument to another function.

A

In this case the function is a callback (because you call-it-back within the code-block of the calling function).

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

What is function hoisting in JavaScript.

A

Function hoisting is when function declarations are moved to the top of the scope regardless of their place in the code.

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

Should we rely on function hoisting?

A

No, we should always declare our functions before invoking them.

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

What is a function expression?

A

A function expression is when a variable is defined using an anonymous function.

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

Are function expressions hoisted?

A

No, variables defined with a function are not hoisted in the scope.

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

What is the syntax of an arrow function?

A

(param1, param2) => { code-block }

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

When do we need to use parenthesis around parameters in an arrow function?

A

When there are zero, or more than one parameter.

18
Q

What is the syntax for an arrow function with one parameter?

A

param1 = > {code-block}

19
Q

If your arrow function has zero parameters, what can you replace the parenthesis with?

A

An underscore _

20
Q

How do we use implicit returns in arrow functions

A

Implicit returns are used when the function occupies one line of code and includes no curly braces { }

21
Q

What data type are functions in JavaScript?

A

Functions are objects in JavaScript.

22
Q

How is it useful that functions are objects in JavaScript?

A

Because functions are objects, they can be a value to an object property.

23
Q

What is another name for a value of an object property (or key) that is also a function?

A

These functions are called methods.

24
Q

What is the syntax for calling a method?

A

objectName.propertyName()

or

objectName‘propertyName’

The parenthesis are needed to invoke the function

25
Q

Can methods include parameters?

A

Yes, the definition of a function as a method allows for parameters?

26
Q

Can you pass methods arguments?

A

Yes, depending on the method, they will accept arguments.

27
Q

Can you use functions as a variable?

A

Yes, functions can defined as a variable - that is a function expression.

28
Q

What is a callback function?

A

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

29
Q

Give an example of a commonly used callback function.

A

The event handler of an event listener is a callback function, it is passed to the eventlistener to be called once an event has occured

30
Q

How could we rewrite an event listener to explicitly show that a callback is being used?

A

function callback(){do code}

button.addEventListener(‘click’, callback)

31
Q

What are two situations where we might want to use callback functions?

A
  1. When we want to allow code to be easily swapped
  2. To prevent blocking situations in asynchronous code
32
Q

What are two benefits of callback functions?

A

They allow code to be easily swapped by changing which callback is used.

Callback functions prevent blocking operations in asynchronous code

33
Q

How can we easily swap code using a callback function?

A

If we create a function expression we can then pass the expression into the function as a callback. By changing which expression we use we swap which code is run during the callback

34
Q

Give an example of commonly seen asynchronous code in JavaScript.

A

An event listener is an example of asynchronous code because it does not execute immediately when found in the code.

35
Q

What is synchronous code in JavaScript?

A

Synchronous code runs in a top-to-bottom, left-to-right manner in the executable file.

36
Q

What is the idea of JavaScript being single threaded?

A

The idea is that JavaScript can only be performing one block of code at a time.

37
Q

What is blocking in JavaScript?

A

Blocking is the behavior of only running one part of a code base at a time. If something has not completed, nothing else can be undertaken.

38
Q

What is the event loop in JavaScript?

How many parts does the event loop have?

A

The event loop has three parts:

  1. The call-stack: basically a to-do list of code that needs to run
  2. The web-api: a waiting-list of code that is awaiting an event
  3. The event queue: when a waiting-list result happens the continuation step gets added to the event queue.

The event loop is how JavaScript decides what it need to execute at any given time.

39
Q

How can we set a default value for a parameter in a function?

A

We can set the parameter = to some value:

function sayName(name = ‘Stranger’){ }

40
Q

What is a helper function?

A

A helper function is a function called within another function’s code body

41
Q

How do we pass arguments to a helper function

A

We can pass arguments to a helper function by defining them as parameters in the ‘base’ function. The parameters must have the same name as in the helper function.