ES6 Flashcards

1
Q

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

A

Code blocks are denoted by curly braces {} (the space between the braces)
Function code blocks, conditional code blocks, loop code blocks
(With the exception of objects)

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

What does block scope mean?

A

The scope is only within the code block, and block-scoped variables (declared with let or const) are separate entities from those declared outside the block
var variables are accessible outside of a code block EXCEPT for function code blocks

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

block scoped

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

let values can be reassigned while const values cannot
However, objects assigned to const variables are still mutable, but the object that a const variable is referring to cannot be changed

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

The variable has the same memory address as its value, referring to a certain array. The array is mutable but the location in memory remains constant
The name binding cannot be changed
The array remains constant, not the elements within it

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

Just use let and const from now on
If a variable will be changed (incremented, reassigned, appended to when it’s a primitive), use let rather than const

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

Wrap a string in backticks ` instead of quotes

Interpolations are denoted with ${ }

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

What is “string interpolation”?

A

Substituting parts of a string with the values of variables or expressions
Inserting values into a string (interpolation is the insert something of a different nature into something else)
In the context of template literals, Hello my name is ${nameVariable}. the value of variable nameVariable is interpolated into the string

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

What are tagged templates?

A

Tag templates allow a function to be applied to the string interpolation by prefacing the template literal with the function name let greeting = myFunc`Hi`;

Template tag functions should be like
```javascript
function tag(literals, ...substitutions) { 
	// literals will be an array, holds the strings delimited by the substitutions
	// substitutions is also an iterable holding the value of the variables that are being subtituted/interpolated in
	// return a string from the function
} 

~~~

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

What can template literals be used for?

A
  • String interpolation
  • Multiline strings without having to use slashes and newline characters
  • Inserting HTML
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is destructuring, conceptually?

A

Taking the properties of an object (and similarly, the elements of an array) and assigning them to multiple variables in one go
Taking the structure of an object (or array) and breaking that structure down to get at the inner components

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

What is the syntax forObjectdestructuring?

A

```javascript
const { // const can be let or var
property1InMyObj: variableToStoreProp1,
property2InMyObj: variableToStoreProp2,
propertyWithNesting: {
nestedProp1: varToStoreNested1,
nestedProp2: varToStoreNested2 = ‘default value if nestedProp2 does not exist’
}
} = myObj;
// We can also do = myObj || {} if myObj might be null. This makes all the variables that would have come out === undefined instead of throwing an error
~~~

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

What is the syntax forArraydestructuring?

A

```javascript
let [ // let can be const or var
variableForIdx0,
variableForIdx1,
variableForIdx2,
[
variableForNestedIdx0,
variableForNestedIdx1 = ‘default value if no value at nested index 1’,
variableForNestedIdx2 = ‘default value if no value at nested index 2’
]
] = myArr;
// We can also do = myArr || [] as a fallback for if myArr might be null. This makes all the variables === undefined instead of throwing an error just like object destructuring
~~~

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

How can you tell the difference between destructuring and creatingObject/Arrayliterals?

A
Destructuring looks like an `Object` or `Array` literal, but it will be on the left side of an assignment operator (in the space for "where things get stored")
The `Object`/`Array` literals will be on the right side (in the space for "what will be assigned to something")
_e.g._ `let { property: newVar } = object` vs. `let object = { property: newVar }`
More subtle example, when destructuring is used for function params
```javascript
//These two functions do the same thing given object person
let exPerson = {
	firstName: 'John',
	lastName: 'Doe'
};
function display(personObj) {
	console.log(personObj.firstName, personObj.lastName)
}
function display({firstName: varFirstName, lastName: varLastName}) {
	console.log(varFirstName, varLastName);
}
// You aren't passing in an object literal as a parameter. You are setting the parameter to the argument. The following is happening, which is how we can tell we're working with a destructuring:
// myParam = passedArg
// {firstName: varFirstName, lastName: varLastName} = person
// This ^ is the syntax for a destructuring!

~~~

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

What is the syntax for defining an arrow function?

A

Instead of function (param1, param2) { //stuff } it is (param1, param2) => { //stuff }

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

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

A

The arrow function returns value of the expression of the (now curly braces-less body)
An implicit return (Tim calls that part a “return expression”)

17
Q

How is the value ofthisdetermined within an arrow function?

A

While regular function syntax will create a new function that creates its own this, arrow functions do not create their own this. Instead, it will be lexically scoped, so it will find the this for the context that it is within
They don’t create their own this binding
Value of this is defined at definition time, not at call time (which is the case for traditional ES5 functions)
If you can’t see where it’s being called, this might be referring to the global window

18
Q

What are the three states a Promise can be in?

A
  • pending
    • fulfilled
    • rejected
19
Q

How do you handle the fulfillment of a Promise?

A

Pass a function as the first argument to a .then method on the Promise

20
Q

How do you handle the rejection of a Promise?

A

Either pass a function as the second argument to a .then method, or a function as the only argument in a .catch method

21
Q

What is Array.prototype.filter useful for?

A

It is useful for extracting out values from an Array in a simple and repeatable way, and is particularly useful when extracting out values based on criteria other than matching the values themselves

22
Q

What is Array.prototype.map useful for?

A

It is useful for manipulating/transforming elements inside an array (and getting a new array)

23
Q

What is Array.prototype.reduce useful for?

A

It is useful for “aggregating” elements in an array into a single entity, and the aggregation does not have to be simply mathematical operations on numbers. It can be used any time we have an array of things that we want to do stuff to, and we want the end result to be a single thing.

24
Q

What is “syntactic sugar”?

A

Syntax that makes code easier/”sweeter” to read for humans but can be removed without affecting the capabilities of the language (extra stuff to make things easier to work with)

25
Q

What is thetypeofan ES6 class?

A
  • The class is a function, more precisely a constructor function
  • ES6 classes are syntactic sugar for constructor functions
26
Q

Describe ES6 class syntax.

A
  • class keyword followed by the name of the class, then curly braces. Inside the braces, a function constructor which is called when a new object of that class is instantiated. The constructor function is followed by method definitions written like function definitions
    • ```javascript
      class Student {
      constructor(firstName, lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
      }
      getFullName() {
      return ${this.firstName} ${this.lastName};
      }
      }
      ~~~
27
Q

What is “refactoring”?

A

Refactoring is rewriting code without affecting the functionality/behavior of the program, usually to make code cleaner and more maintainable

28
Q

How are ES Modules different from CommonJS modules?

A
  • They have a different syntax (import and export instead of require() and module.exports)
    • ES Modules are official and are supported by browsers and “other JavaScript runtimes”
      • Node.js (a runtime) used to use CommonJS syntax while others used ECMAScript modules
29
Q

What kind of modules can Webpack support?

A

Webpack supports ECMAScript modules as well as CommonJS modules (AMD, async module definitions, all out of the box), and can support even more with loaders

30
Q

What doesfetch()return?

A

A Promise that only resolves to reject if there is a network error that prevents the request from completing, otherwise a response object

31
Q

What is the default request method used byfetch()?

A

GET

32
Q

How do you specify the request method (GET,POST, etc.) when callingfetch?

A

After the URL, specify in the options object { method: 'GET' }