45. What does this return?
const firstPromise = new Promise((res, rej) => {
setTimeout(res, 500, 'one'); });
const secondPromise = new Promise((res, rej) => {
setTimeout(res, 100, 'two'); }); Promise.race([firstPromise, secondPromise]).then(res => console.log(res));
DevScrolls
Whats the output?
Answer: B
When we pass multiple promises to the Promise.race method, it resolves/rejects the first promise that resolves/rejects. To the setTimeout method, we pass a timer: 500ms for the first promise (firstPromise), and 100ms for the second promise (secondPromise). This means that the secondPromise resolves first with the value of 'two'. res now holds the value of 'two', which gets logged.
When we pass multiple promises to the Promise.race method, it resolves/rejects the first promise that resolves/rejects. To the setTimeout method, we pass a timer: 500ms for the first promise (firstPromise), and 100ms for the second promise (secondPromise). This means that the secondPromise resolves first with the value of 'two'. res now holds the value of 'two', which gets logged.
46. What's the output?
let person = { name: 'Lydia' }; const members = [person]; person = null; console.log(members);
let person = { name: 'Lydia' }; const members = [person]; person = null; console.log(members);
Anonymous Poll
26%
A: null
24%
B: [null]
12%
C: [{}]
38%
D: [{ name: "Lydia" }]
DevScrolls
46. What's the output?
let person = { name: 'Lydia' }; const members = [person]; person = null; console.log(members);
let person = { name: 'Lydia' }; const members = [person]; person = null; console.log(members);
Answer: D
First, we declare a variable person with the value of an object that has a name property.

Then, we declare a variable called members. We set the first element of that array equal to the value of the person variable. Objects interact by reference when setting them equal to each other. When you assign a reference from one variable to another, you make a copy of that reference. (note that they don't have the same reference!)

Then, we set the variable person equal to null.

We are only modifying the value of the person variable, and not the first element in the array, since that element has a different (copied) reference to the object. The first element in members still holds its reference to the original object. When we log the members array, the first element still holds the value of the object, which gets logged.
First, we declare a variable person with the value of an object that has a name property.

Then, we declare a variable called members. We set the first element of that array equal to the value of the person variable. Objects interact by reference when setting them equal to each other. When you assign a reference from one variable to another, you make a copy of that reference. (note that they don't have the same reference!)

Then, we set the variable person equal to null.

We are only modifying the value of the person variable, and not the first element in the array, since that element has a different (copied) reference to the object. The first element in members still holds its reference to the original object. When we log the members array, the first element still holds the value of the object, which gets logged.
47. What's the output?
const person = { name: 'Lydia', age: 21, }; for (const item in person) { console.log(item); }
const person = { name: 'Lydia', age: 21, }; for (const item in person) { console.log(item); }
Anonymous Poll
38%
A: { name: "Lydia" }, { age: 21 }
21%
B: "name", "age"
33%
C: "Lydia", 21
8%
D: ["name", "Lydia"], ["age", 21]
DevScrolls
47. What's the output?
const person = { name: 'Lydia', age: 21, }; for (const item in person) { console.log(item); }
const person = { name: 'Lydia', age: 21, }; for (const item in person) { console.log(item); }
Answer: B
With a for-in loop, we can iterate through object keys, in this case name and age. Under the hood, object keys are strings (if they're not a Symbol). On every loop, we set the value of item equal to the current key it’s iterating over. First, item is equal to name, and gets logged. Then, item is equal to age, which gets logged.
With a for-in loop, we can iterate through object keys, in this case name and age. Under the hood, object keys are strings (if they're not a Symbol). On every loop, we set the value of item equal to the current key it’s iterating over. First, item is equal to name, and gets logged. Then, item is equal to age, which gets logged.
48. What's the output?
console.log(3 + 4 + '5');
console.log(3 + 4 + '5');
Anonymous Poll
13%
A: "345"
67%
B: "75"
13%
C: 12
7%
D: "12"
DevScrolls
48. What's the output?
console.log(3 + 4 + '5');
console.log(3 + 4 + '5');
Answer: B
Operator associativity is the order in which the compiler evaluates the expressions, either left-to-right or right-to-left. This only happens if all operators have the same precedence. We only have one type of operator: +. For addition, the associativity is left-to-right.
3 + 4 gets evaluated first. This results in the number 7.
7 + '5' results in "75" because of coercion. JavaScript converts the number 7 into a string, see question 15. We can concatenate two strings using the +operator. "7" + "5" results in "75".
Operator associativity is the order in which the compiler evaluates the expressions, either left-to-right or right-to-left. This only happens if all operators have the same precedence. We only have one type of operator: +. For addition, the associativity is left-to-right.
3 + 4 gets evaluated first. This results in the number 7.
7 + '5' results in "75" because of coercion. JavaScript converts the number 7 into a string, see question 15. We can concatenate two strings using the +operator. "7" + "5" results in "75".
49. What's the value of num?
const num = parseInt('7*6', 10);
const num = parseInt('7*6', 10);
Anonymous Poll
34%
A: 42
20%
B: "42"
6%
C: 7
40%
D: NaN
DevScrolls
49. What's the value of num?
const num = parseInt('7*6', 10);
const num = parseInt('7*6', 10);
Answer: C
Only the first numbers in the string is returned. Based on the radix (the second argument in order to specify what type of number we want to parse it to: base 10, hexadecimal, octal, binary, etc.), the parseInt checks whether the characters in the string are valid. Once it encounters a character that isn't a valid number in the radix, it stops parsing and ignores the following characters.
* is not a valid number. It only parses "7" into the decimal 7. num now holds the value of 7.
Only the first numbers in the string is returned. Based on the radix (the second argument in order to specify what type of number we want to parse it to: base 10, hexadecimal, octal, binary, etc.), the parseInt checks whether the characters in the string are valid. Once it encounters a character that isn't a valid number in the radix, it stops parsing and ignores the following characters.
* is not a valid number. It only parses "7" into the decimal 7. num now holds the value of 7.
50. What's the output?
[1, 2, 3].map(num => { if (typeof num === 'number') return; return num * 2; });
[1, 2, 3].map(num => { if (typeof num === 'number') return; return num * 2; });
Anonymous Poll
33%
A: []
33%
B: [null, null, null]
29%
C: [undefined, undefined, undefined]
5%
D: [ 3 x empty ]
👍2
DevScrolls
50. What's the output?
[1, 2, 3].map(num => { if (typeof num === 'number') return; return num * 2; });
[1, 2, 3].map(num => { if (typeof num === 'number') return; return num * 2; });
Answer: C
When mapping over the array, the value of num is equal to the element it’s currently looping over. In this case, the elements are numbers, so the condition of the if statement typeof num === "number" returns true. The map function creates a new array and inserts the values returned from the function.
However, we don’t return a value. When we don’t return a value from the function, the function returns undefined. For every element in the array, the function block gets called, so for each element we return undefined.
When mapping over the array, the value of num is equal to the element it’s currently looping over. In this case, the elements are numbers, so the condition of the if statement typeof num === "number" returns true. The map function creates a new array and inserts the values returned from the function.
However, we don’t return a value. When we don’t return a value from the function, the function returns undefined. For every element in the array, the function block gets called, so for each element we return undefined.
51. What's the output?
function getInfo(member, year) {
member.name = 'Lydia';
year = '1998';
}
const person = {
name: 'Sarah'
};
const birthYear = '1997';
getInfo(person, birthYear);
console.log(person, birthYear);
DevScrolls
51. What's the output? function getInfo(member, year) { member.name = 'Lydia'; year = '1998'; } const person = { name: 'Sarah' }; const birthYear = '1997'; getInfo(person, birthYear); console.log(person, birthYear);
DevScrolls
Output?
Answer: A
Arguments are passed by value, unless their value is an object, then they're passed by reference. birthYear is passed by value, since it's a string, not an object. When we pass arguments by value, a copy of that value is created (see question 46).
The variable birthYear has a reference to the value "1997". The argument year also has a reference to the value "1997", but it's not the same value as birthYear has a reference to. When we update the value of year by setting year equal to "1998", we are only updating the value of year. birthYear is still equal to "1997".
The value of person is an object. The argument member has a (copied) reference to the same object. When we modify a property of the object member has a reference to, the value of person will also be modified, since they both have a reference to the same object. person's name property is now equal to the value "Lydia"
Arguments are passed by value, unless their value is an object, then they're passed by reference. birthYear is passed by value, since it's a string, not an object. When we pass arguments by value, a copy of that value is created (see question 46).
The variable birthYear has a reference to the value "1997". The argument year also has a reference to the value "1997", but it's not the same value as birthYear has a reference to. When we update the value of year by setting year equal to "1998", we are only updating the value of year. birthYear is still equal to "1997".
The value of person is an object. The argument member has a (copied) reference to the same object. When we modify a property of the object member has a reference to, the value of person will also be modified, since they both have a reference to the same object. person's name property is now equal to the value "Lydia"
52. What's the output?
function greeting() {
throw 'Hello world!';
}
function sayHi() {
try {
const data = greeting(); console.log('It worked!', data);
} catch (e) {
console.log('Oh no an error:', e);
}
}
sayHi();
DevScrolls
52. What's the output? function greeting() { throw 'Hello world!'; } function sayHi() { try { const data = greeting(); console.log('It worked!', data); } catch (e) { console.log('Oh no an error:', e); } } sayHi();
DevScrolls
52. What's the output? function greeting() { throw 'Hello world!'; } function sayHi() { try { const data = greeting(); console.log('It worked!', data); } catch (e) { console.log('Oh no an error:', e); } } sayHi();
Answer: D
With the throw statement, we can create custom errors. With this statement, you can throw exceptions. An exception can be a string, a number, a boolean or an object. In this case, our exception is the string 'Hello world!'.
With the catch statement, we can specify what to do if an exception is thrown in the try block. An exception is thrown: the string 'Hello world!'. e is now equal to that string, which we log. This results in 'Oh an error: Hello world!'.
With the throw statement, we can create custom errors. With this statement, you can throw exceptions. An exception can be a string, a number, a boolean or an object. In this case, our exception is the string 'Hello world!'.
With the catch statement, we can specify what to do if an exception is thrown in the try block. An exception is thrown: the string 'Hello world!'. e is now equal to that string, which we log. This results in 'Oh an error: Hello world!'.
53. What's the output?
A: "Lamborghini"
B: "Maserati"
C: ReferenceError
D: TypeError
function Car() {
this.make = 'Lamborghini'; return {
make: 'Maserati'
};
}
const myCar = new Car(); console.log(myCar.make);A: "Lamborghini"
B: "Maserati"
C: ReferenceError
D: TypeError