Javascript Flashcards

1
Q

What’s the difference between setInterval and setTimer?

A

setTimer will fire once after the duration, setInterval will continually run.. both return IDs that can be used to clear or cancel them.

setInterval will continue if an error happens.

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

What is bind ?

A

Bind is called on a function and return a new function with ‘this’ bound to the first argument.

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

Whats the difference between call and apply?

A

both will call a function immediately w/ a first arg to use as ‘this’. The difference is in remaining args, apply takes an array of args, whereas call take comma delimited args like a traditional func all.

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

what are javascript classes?

A

Introduced in ES 6 they provide a more familar way to define classes over the prototypical way.

class Bike{
getWheelCount(){ return 2}
}

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

what are some uses of the spread operator?

A

creating copies (be careful they are shallow) copies of objects and arrays.

merging objects and concatenating arrays

unpacking arrays into multiple args for a function

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

how do you do a deep copy of a var?

A

JSON.stringify into JSON.parse

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

what’s a pitfall of checking typeof bar === “object” ?

A

nulls are also an object so you must check !== null

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

what are benefits of ‘use strict’

A

will avoid accidental global vars that are made without var keyword.

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

what is a closure?

A

Closer is a function that returns a function, but sets some state in the outer function that is referenced in the inner function.

A good example is a createCounter method that might return something that can be called like counter() and output increasingly numbers for each call.

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

what are the three states of a promise

A

pending, fulfilled and rejected

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

how do you create a new promise?

A

const promise = new Promise((resolve, reject) => {
// call reject or resolve
})

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

what is the modern alternative to promises?

A

using async and await.. it allows the code to read more impertively and avoid ‘callback hell’.. functions declared w/ async can call await inside async calls to wait for the function to finish before running the next line.

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