JavaScript ES6 Flashcards

1
Q

What is a code block? What are some examples of a code block?

A

A code block is a section of code that is denoted by curly braces ( { } ), also the code in parentheses of the for loop. Some examples of a code block would be functions, if else statements, for loops, while loops, etc.

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

What does block scope mean?

A

Block scope means that it is only accessible within the code block. What happens inside the code block stays inside the code block.

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

What is the scope of a variable declared with const or let?

A

Variables declared with const or let are all blocked-scope variables.

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

What is the difference between let and const?

A

The difference is that const is a read-only reference to a value and are immutable, or can’t be reassigned, while let declarations are mutable, which means you can change their values anytime.

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

Why is it possible to .push() a new value into a const variable that points to an Array?

A

This is because even though the Array is immutable, its value or its contents are not.

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

How should you decide on which type of declaration to use?

A

Each declaration should be const, unless you want them to be mutable, in which case you should use go back and change to let. Never var.

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

What is the syntax for writing a template literal?

A

Use backticks (``) instead of single or double quotes. ${…} is used for expressions (variables, functions, etc.). Only use template literal if you will use substitution.

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

What is “string interpolation”?

A

The JavaScript engine will automatically replace these variables and expressions (${…}) with their values.

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

What is destructuring, conceptually?

A

It “destructures” something by giving a name to, or assigning to a variable, each of its parts.

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

What is the syntax for Object destructuring?

A

const { prop1: variableName1, prop2: variableName2 } = object;
or if variable name will be the same as property name, then:
const { prop1, prop2 } = object

For nested object properties:
const {
name: {
firstName,
lastName
}
} = employee;
to get firstName and lastName.

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

What is the syntax for Array destructuring?

A

const [x, y, z] = array;

const [
firstName,
lastName,
[
color1,
color2,
color3
]
] = getProfile();
to get colo1, colo2, and color3.

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

How can you tell the difference between destructuring and creating Object/Array literals?

A

Creating Object/Array literals will have the variable name to the left of the assignment operator while destructuring Object/Array literals will have the variable name to the right of the assignment operator.

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

What is the syntax for defining an arrow function?

A

Instead of
function (param) {
return value;
}

param => value;

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

When an arrow function’s body is left without curly braces, what changes in its functionality?

A

It returns the function body. It also won’t be able to have an object literal since both block and object literal use curly brackets, so JavaScript can’t distinguish between a block and an object. Instead, you must wrap the object literal in parentheses.

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

How is the value of this determined within an arrow function?

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

What are the three states a Promise can be in?

A
  1. pending: initial state, neither fulfilled nor rejected.
  2. fulfilled: meaning that the operation was completed successfully.
  3. rejected: meaning that the operation failed.
17
Q

How do you handle the fulfillment of a Promise?

A

.then(onFulfillment)

18
Q

How do you handle the rejection of a Promise?

A

.then(…, onRejection)
or
.catch(onRejection)

19
Q

What does Array.filter do?

A

The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

20
Q

What should the callback function return?

A

A boolean, true or false.

21
Q

What is Array.filter useful for?

A

It is useful for creating a new array that contains elements that you care about or pass a test case.

22
Q

What does Array.map do?

A

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

23
Q

What should the callback function return?

A

An array element that has been mutated with the provided function.

24
Q

What is Array.map useful for?

A

It is useful for creating a new array that has had something done to each element from the original array. Change and array into react components.

25
Q

What does Array.reduce do?

A

The reduce() method executes a user-supplied “reducer” callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

The first time that the callback is run there is no “return value of the previous calculation”. If supplied, an initial value may be used in its place. Otherwise the array element at index 0 is used as the initial value and iteration starts from the next element (index 1 instead of index 0).

26
Q

What action should the callback function perform?

A

The callback function executes on each element of the array, in order, passing in the return value from the calculation on the preceding element.

27
Q

What should the callback function return?

A

The final result or return of running the reducer across all elements of the array is a single value.

28
Q

What is Array.reduce useful for?

A

Using a function on an array to get a single return that can be completed with and is related to each element in the array.

29
Q

What are JavaScript classes?

A

The basis of Object Oriented Programming (OOP), they describe the behavior of a collection of similar objects. That is, a class describes the methods and properties that these objects all share. In addition, classes support inheritance, meaning that you can extend a class and add additional methods or properties. A class is not an object, but is a template for creating an object. The new operator is used to create objects (“instances”) of a class.

30
Q

When would you want to use a class?

A

When you want to make objects that have similar properties.

31
Q

How do you declare a class?

A

class Shape {
constructor(area, circumference) {
this.area = area;
this.circumference = circumference;
}

print() {
return area: ${this.area}, circumference: ${this.circumference};
}
}

const shape = new Shape(1, 4);
console.log(shape.print());

32
Q

How do you inherit from another class?

A

class Circle extends Shape {
constructor(radius) {
super(Math.PI * radius * radius, 2 * Math.PI * radius);
this.radius = radius;
}

print() {
return ${super.print()}, radius: ${this.radius};
}
}

const circle = new Circle(1);
console.log(circle.print());

33
Q

Why would you want to inherit from another class?

A

You would want to inherit from another class if you want objects to “borrow” a large part of another object’s behaviors, while overriding or enhancing certain parts with its own logic.

34
Q

How do you add methods and properties to a class?

A

You add methods by declaring a function in the class code block and ass properties by using this.property = value.