Syntax Flashcards
(20 cards)
What is the JavaScript syntax for a for loop?
for (var x in y) {
doSomething()
}
How would you find a remove a specific element of an array in JavaScript?
const index = myArray.indexOf(specificElement)
if ( index !== -1 ) {
array.splice(specificElement, 1)
} else {
console.log(“Item wasn’t in the array.”);
}
In JavaScript, how would you import a specific value from the JSON inside of another file (i.e. “keys.json”)?
const myValue = require(“./config/keys”).specificKey;
What are the two outcomes of a JavaScript “promise”?
The promise is “resolved” or fails and is “rejected”
What is the syntax for a JavaScript promise?
let p = new Promise ((resolve, reject) => { let a = 1; if (a == 1) { resolve('Success') } else { reject('Failed') } })
In JavaScript, when does “myPromise.then( () => { console.log(‘Test’) })” run?
If myPromise resolves
In JavaScript, what is the syntax for a then statement after a promise?
p.then( (message) => {
console.log(‘This is in the then ‘ + message)
}).catch( (message) => {
console.log(‘This is in the catch ‘ + message)
})
In JavaScript, when does the “catch” run in a “then” statement?
If the promise is rejected (fails)
In JavaScript, why are promises “cleaner” than callbacks?
Promises avoid “callback hell”, which is nesting callbacks. Instead, a series of “then” statements can be used once one call resolves
In JavaScript, why would you want to run Promise.all([])?
If some of the promises are slower than others, they do run concurrently without having to wait for the slow promises to finish
In JavaScript, what does Promise.race([]) do?
It runs a series of promises concurrently and returns the resolve object from the first one to finish
What is module.exports in JavaScript?
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
What are modules in JS?
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
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]?
const [a, b] = myArray;
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’?
const [a,, b, …rest] = myArray;
In JavaScript, how would you make an array including just the items of two other arrays without having any nested items?
Use the spread operator: const myArray = [...firstArray, ...secondArray];
OR
Use the "concat" array method: const myArray = firstArray.concat(secondArray);
How would you use JavaScript destructuring to name two variables, ‘left’ and ‘right’, from the returned list of function ‘returnLeftAndRight()’
const [left, right] = returnLeftAndRight();
In JavaScript, what does the “filter” array method take for parameters?
a name for each element in the array and then a condition used for filtering.
myArray.filter( item => item.id !== currentId );
How would you use the JavaScript filter array method to filter out one item from an array?
myArray = myArray.filter( item => item.id !== currentId );
In JavaScript, what is the syntax for the “bind” function?
let x = { cost: 10 };
function myFunc () { console.log(this.cost); }
boundFunc = myFunc.bind(x)
// prints 10