Syntax Flashcards

(20 cards)

1
Q

What is the JavaScript syntax for a for loop?

A

for (var x in y) {
doSomething()
}

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

How would you find a remove a specific element of an array in JavaScript?

A

const index = myArray.indexOf(specificElement)
if ( index !== -1 ) {
array.splice(specificElement, 1)
} else {
console.log(“Item wasn’t in the array.”);
}

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

In JavaScript, how would you import a specific value from the JSON inside of another file (i.e. “keys.json”)?

A

const myValue = require(“./config/keys”).specificKey;

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

What are the two outcomes of a JavaScript “promise”?

A

The promise is “resolved” or fails and is “rejected”

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

What is the syntax for a JavaScript promise?

A
let p = new Promise ((resolve, reject) => {
    let a = 1;
    if (a == 1) {
        resolve('Success')
    }
    else {
        reject('Failed')
    }
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In JavaScript, when does “myPromise.then( () => { console.log(‘Test’) })” run?

A

If myPromise resolves

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

In JavaScript, what is the syntax for a then statement after a promise?

A

p.then( (message) => {
console.log(‘This is in the then ‘ + message)
}).catch( (message) => {
console.log(‘This is in the catch ‘ + message)
})

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

In JavaScript, when does the “catch” run in a “then” statement?

A

If the promise is rejected (fails)

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

In JavaScript, why are promises “cleaner” than callbacks?

A

Promises avoid “callback hell”, which is nesting callbacks. Instead, a series of “then” statements can be used once one call resolves

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

In JavaScript, why would you want to run Promise.all([])?

A

If some of the promises are slower than others, they do run concurrently without having to wait for the slow promises to finish

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

In JavaScript, what does Promise.race([]) do?

A

It runs a series of promises concurrently and returns the resolve object from the first one to finish

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

What is module.exports in JavaScript?

A

An object that every JS file has that is the only accessible data from within the file. If any data needs to be exported, you may set the object to whatever needs to be accessed

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

What are modules in JS?

A

Modules are individual files that encapsulate a part of the program as a whole, which must export any blocks of code that must be used by another file

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

const myArray = [‘a1, ‘b2’. ‘c3’];

In JavaScript, how would you destructure this list to set the ‘a’ variable equal to myArray[0] and ‘b’ equal to myArray[1]?

A

const [a, b] = myArray;

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

What is the syntax for JavaScript destructuring an array (myArray) to set the first item to the ‘a’ variable, the third item to ‘b’ and the remainder of the items to ‘c’?

A

const [a,, b, …rest] = myArray;

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

In JavaScript, how would you make an array including just the items of two other arrays without having any nested items?

A
Use the spread operator:
const myArray = [...firstArray, ...secondArray];

OR

Use the "concat" array method:
const myArray = firstArray.concat(secondArray);
17
Q

How would you use JavaScript destructuring to name two variables, ‘left’ and ‘right’, from the returned list of function ‘returnLeftAndRight()’

A

const [left, right] = returnLeftAndRight();

18
Q

In JavaScript, what does the “filter” array method take for parameters?

A

a name for each element in the array and then a condition used for filtering.

myArray.filter( item => item.id !== currentId );

19
Q

How would you use the JavaScript filter array method to filter out one item from an array?

A

myArray = myArray.filter( item => item.id !== currentId );

20
Q

In JavaScript, what is the syntax for the “bind” function?

A

let x = { cost: 10 };

function myFunc () { console.log(this.cost); }

boundFunc = myFunc.bind(x)

// prints 10