The React Handbook Flashcards

1
Q

Are variables in JavaScript typed?

A

No, once you assign a specific literal type to a variable, you can later reassign the variable to house another type without any type errors or issues.

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

What is the scope?

A

The scope is the portion of code where a variable is visible.

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

How is a variable initialized with ‘var’ outside of a function assigned?

A

It is assigned to the global object and has a global scope and is visible anywhere.

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

How is a variable initialized inside a function assigned?

A

It is assigned to that function. It’s local and is visible only inside the function, just as it it were a function parameter.

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

Does a block (Identified by a pair of curly braces) define a new scope for a variable defined by ‘var’?

A

Nah, dawg. A new scope is only created when a function is created, because var does not have block scope but function scope.

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

What is hoisting?

A

JS actually moves all variable declarations to the top before executing the code. This is why variables defined anywhere in the function are available, even in lines of code above them. To avoid “what the heck is that variable?” types of confusion, however, we should always declare our variables at the beginning of a function.

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

What is let?

A

It’s a block-scoped version of var - that is, its scope is limited to teh block, statement or expression where it’s defined, and all the contained inner blocks

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

What happens if you define let outside of any function?

A

Unlike var, it does not create a global variable.

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

What’s unique about a const, once it is initialized?

A

Its value can’ tbe changed again and it can’t be reassigned to a different value.

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

What’s the catch about reassigning consts?

A

We can’t reassign a const, but we can mutate it, if it is an object that provides methods to mutate the object’s contents.

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

What sort of scope does Const have?

A

Block scope, same as let.

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

Why would a developer choose to only use const and let?

A

Because we should always use the simplest construct available to avoid errors down the road.

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

Convert the following to an arrow function:

const myFunction = function() {
  //...
}
A
const myFunction = () => {
  //...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
When can you omit the curly braces and write everything on a single line, like this:
const myFunction = () => doSomething();
A

Whenever the function body is just a single statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
When can you omit the parentheses completely? Like this:
const myFunction = param => doSomething(param)
A

When you have one (and only one) param.

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

What sort of returns are you allowed with arrow functions?

A

Implicit returns - that is, within an arrow function, values are returned without using the return keyword.

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

When can you use implicit returns within an arrow function?

A

When there is a one-line statement for the function body. So,
const myFunction - () => ‘test’

will return ‘test’, even though we don’t use the keyword.

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

What happens if we return an object from a one-liner arrow function, and don’t wrap it in parentheses?

A

Those braces are considered the wrapping function body brackets, instead of denoting an object.

So it should instead look like:
const myFunction = () => ({ value: 'test' })
19
Q

In a regular function, what does ‘this’ refer to?

A

the object - so if you call ‘this.manufacturer’ within a car object, you’ll get the car object your in’s manufacturer.

20
Q

What is ‘this’ within an arrow function?

A

It inherits the ‘this’ scope from the execution context. Arrow functions don’t bind ‘this’ at all, so its value will be whatever is looked up in the call back.

21
Q

When should we NOT use Arrow Functions (due to needing this to be bound to the calling object)

A

Within object methods, constructors, or when handling events - because DOM event listeners set ‘this’ to be the target element.

22
Q

What can be expanded using a spread operator?

A

An array, an object, or a string.

23
Q

What does the spread operator (…) do?

A

It takes every single item from an iterable (arrays, strings, DOM nodes, objects, maps, sets) and applies it to the containing array.

24
Q
How should you combine two arrays like so:
const featured = [‘Deep Dish’, ‘Pepperoni’, ‘Hawaiian’];
const specialty = [‘Metzza’, ‘Spicy Mama’, ‘Margherita’]’
A

const pizzas = […featured, …specialty]

25
Q

How can you make an array from
const featured = [‘Deep Dish’, ‘Pepperoni’, ‘Hawaiian’];
const specialty = [‘Metzza’, ‘Spicy Mama’, ‘Margherita’]’
and add a ‘veg’ pizza in the middle?

A

const pizzas = […featured, ‘veg’, …specialty]

26
Q

What’s wrong with copying an array like so:
let pizzas = [‘pep’, ‘cheese’]
let fridayPizzas = pizzas

A

Any changes you make to fridayPizzas will also change pizzas, because you haven’t actually copied the array, you’ve only named a reference to it.

27
Q

What’s the correct way to copy an array to a new array?

A
Use spread:
const fridayPizzas = [...pizzas]

This is a new array - changes to fridayPizzas will not affect the pizzas array.

28
Q
If I have:
const inventors = [‘Einstein’, ‘Newton’, ‘Galileo’];
const newInventors = [‘Musk’, ‘Jobs’];

will inventors.push(newInventors) combine the two?

A

Not exactly. It will add an array as a fourth element to inventors, which contains the two newInventors, but it won’t add them as just strings.

29
Q

How should you combine two arrays, now that we have the spread operator?

A

inventors.push(…newInventors)

This will spread the array into the push function, and successively push each string onto the array.

30
Q

In addition to a spread operator, what can the … symbol denote?

A

A rest param..which strangely does the opposite of a spread operator.

31
Q

What two places will you use a rest param?

A
  1. ) In a function

2. ) In a destructuring situation

32
Q

Create a convertCurrency function which assumes the first param passed is the rate, and then takes N number of amounts to be modified according to the currency rate.

A
function convertCurrency(rate, …amounts) {
	return amounts.map(amount => amount * rate);
}

convertCurrency(1.54, 10, 23, 52, 1, 56);

This passes the amounts in a rest param, which bundles them all up into an array that we can then loop over

33
Q

What happens in the following code?

const runner = [‘Robb Schuneman’, 123, 5.5, 5, 3, 6, 35];
const [name, id, runs] = runner;
A

the first value passed is assigned to name, the second is assigned as the ID, and then the third value will be assigned to runs. (We should have used a rest param)

34
Q

What happens in the following code?

const runner = [‘Robb Schuneman’, 123, 5.5, 5, 3, 6, 35];
const [name, id, ...runs] = runner;
A

Because we used a rest param, all the runs left in the array will be bundled up into an array And then you can loop over that and use the data.

35
Q

What happens when we use the spread operator on a string?

A

Each letter of the string is put into an array - this can be useful if we need to transform each letter of a span or something.

36
Q

Explain the following code:

const f = (foo, bar) => {}
const a = [1, 2]
f(…a)
A

Because we spread the array into the function, foo will be assigned 1, and bar will be assigned 2.

37
Q

Explain the following code:

const  numbers = [1, 2, 3, 4, 5]
[first, second, …others] = numbers
A

Because we use a rest param, first will be assigned ‘1’, second will be assigned ‘2’, and 3, 4, and 5 will be bundled up into an array called “others”

38
Q

What does destructuring let us do?

A

Extract some values from an object or an array and put them into named variables.

Like so:
const person = {
  firstName: 'Tom',
  lastName: 'Cruise',
  actor: true,
  age: 54 //made up
}
const { firstName: name, age } = person //name: Tom, age: 54
39
Q

How can you skip values in an array when destructuring?

A

Just by using commas and blank spaces to skip over the unneeded values:

const [first, second, , , fifth] = a

40
Q

Rewrite the following code using destructuring instead:

const person = {
  first: 'Robb',
  last: 'Schuneman'
};
const first = person.first;
const second = person.second;
A
const person = {
  first: 'Robb',
  last: 'Schuneman'
};

const { first, last } = person;

41
Q

In the following code, what is going on?

const settings = { width: 300, color: 'black' }
const { width = 100, height = 100, color = 'blue', fontSize = 25} = settings;
A

Default values are being set - if those named properties don’t exist on settings, they will instead revert to the default.

So, we end up with a width of 300, a height of 100, a color of black and a fontSize of 25.

42
Q

If you receive some data in a form like:

const data = ‘Basketball, Sports, 90210, 23’;

What is the easiest way to get those all into named variables?

A

You can destructure these, after splitting them into an array (so long as they’re perfectly separated by the same delimiter):

const [itemName, category, sku, inventory] = data.split(‘,’);

That will put them into an array, and then immediately put them into the variables.

43
Q

If you have two variables that need to swap, the kind of thing you used to do by setting a temp variable..how can you use destructuring to make your life better?

A

[inRing, onSide] = [onSide, inRing]

By placing them in an array and destructuring that array, you’ve simply swapped them, nothing else to be done. Bloody brilliant!

44
Q

How does destructuring sort of let us return multiple values from a function?

A

We can return an object, and destructure it immediately.

function convertCurrency(amount) {
  const converted = {
    USD: amount * 0.76,
    GBP: amount * 0.53,
    AUD: amount * 1.01
};
return converted;
}

const { USD, AUD } = convertCurrency(100);