ES6 Flashcards

1
Q

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

A

a section of a code that are denoted by curly braces

ex: for, do while, if else, while, try catch each has their own code block

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

What does block scope mean?

A

a block scope is code within the curly braces

ex: let and const are block scope

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 scope (the values of const and let variable is within the block scope where they are declared in)

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:
-variables declared by the let keyword are mutable– meaning you reassign their values anytime you want

const:

  • immutable– you can’t reassign them to different values (if you do try to reassign, you get TypeError error)
  • must immediately initialized to a value (if not, you get SyntaxError error)
  • the const keyword ensures that the variable it create is READ-ONLY (BUT it doesn’t mean that the actual value to which the const variable REFERENCE is immutable, meaning you can change the value of its property)

both:

  • block-scoped
  • have TDZ (temporal death zones)
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

you can add to (the list or elements) of the array that the constant points to/references to, you just can’t reassign or redeclare the constant of that array

when you declare with const, it doesn’t make the content in the array immutable, it makes the name of variable/ the assignment immutable

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

if you don’t intend to modify a variable, use const keyword

a good convention to know: use let when we may need to reassign

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

` ` (backticks) for string

${ } for expressions and variables

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

What is “string interpolation”?

A

the ability to substitute part of the string for the values of variables or expressions

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

What is destructuring, conceptually?

A

process of breaking up a structure

in our case, the structure is objects and arrays

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

var declaration keyword, curly braces, property keys, optional aliases, assignment operator, object you’re destructuring

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

variable keyword, open bracket, element names, closing bracket, assignment operator, array you’re destruring

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

the brackets/braces will be on the left for destructuring

and the name of the object or array you’re destructuring will be on the right

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

variable declaration keyword, follow by the name of the function, an equal operator, the function keyword, open and close parenthesis with optional parameters inside, follow by an optional pair of curly braces if your code is more than a line, and your code statement inside

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

you only omit the curly braces if you use an expression in the body of an arrow function (meaning your function is only one line)
so without the curly braces, you don’t need to specify the return keyword, the function will just return the result of the expression

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

in an arrow function, it captures the value of THIS of the enclosing context/ scope instead of creating its own this context

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

pending, fulfilled, rejected (the operation failed)

17
Q

How do you handle the fulfillment of a Promise?

A

by using the then() method and passing in the resolved callback

the moment the callback gets fulled, its done, thats one benefit of using Promises

the second callback is the error callback, but you should use the catch() method for errors instead, because if you have a series of .then() methods in one chain, if anyone of those .then methods fail, we can use the catch method at the end to print out the error

18
Q

How do you handle the rejection of a Promise?

A

by using the catch() method on the Promise object and passing in the error callback (using error.message property)