Senior JS Flashcards

1
Q

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

A

In JavaScript, blocks are denoted by curly braces {} , for example the if else, for, do while, while, try catch and so on:

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

What does block scope mean?

A

variable definition is only valid within the block of code that it was declared in

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 can be reassigned and const can’t

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

It is adding to the array instead of reassigning the variable

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 the variable does not need to to reassigned, use 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

Put it inside backticks, ${} 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

String formatting: 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

Taking out part of an object and assigning it to new variables

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 {
      title,
      author,
      libraryID
    } = book1
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 [book3, book4, book5] = getBooks()

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 side of the assignment operator that the object/array is on

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

(parameters) => code block

() not needed with one parameter
() are needed with no parameter

{} needed for more than one line of code

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 don’t get a return statement

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

This is defined at definition time for arrow functions.

For other functions, this is determined at call time

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: initial state, neither fulfilled nor rejected.
fulfilled: meaning that the operation was completed successfully.
rejected: meaning that the operation failed.

17
Q

How do you handle the fulfillment of a Promise?

A

attach a then method handler with callback function with success message

18
Q

How do you handle the rejection of a Promise?

A

attach a catch method handler with callback function with error message

19
Q

What is Array.prototype.filter useful for?

A

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

20
Q

What is Array.prototype.map useful for?

A

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

21
Q

What is Array.prototype.reduce useful for?

A

To execute a reducer function (that you provide) on each element of the array, resulting in single output value.

22
Q

What is “syntactic sugar”?

A

making code easier to read

23
Q

What is the typeof an ES6 class?

A

function

24
Q

Describe ES6 class syntax.

A
class Stopwatch {
  constructor(elapsedSeconds) {
    this.elapsedSeconds = elapsedSeconds;
  }

tick() {
this.elapsedSeconds++;
}

  getTime() {
    const secondsPerHour = 3600;
    const secondsPerMinute = 60;
    let seconds = this.elapsedSeconds;
    let hours = Math.floor(seconds / secondsPerHour);
    seconds -= (secondsPerHour * hours);
    let minutes = Math.floor(seconds / secondsPerMinute);
    seconds -= (secondsPerMinute * minutes);
    hours = hours.toString().padStart(2, '0');
    minutes = minutes.toString().padStart(2, '0');
    seconds = seconds.toString().padStart(2, '0');
    return `${hours}:${minutes}:${seconds}`;
  }

reset() {
this.elapsedSeconds = 0;
}
}

25
Q

What is “refactoring”?

A

making code more efficient without changing its behavior

26
Q

What must the return value of myFunction be if the following expression is possible?
myFunction()();

A

It returns the function

27
Q
What does this code do?
const wrap = value => () => value;
A

assigns the return of the value function being called twice to the const wrap variable

Not sure, look at video

28
Q

What allows JavaScript functions to “remember” values from their surroundings?

A

Closures