90. What's the output?
class Person { constructor(name) { this.name = name; } } const member = new Person('John'); console.log(typeof member);
class Person { constructor(name) { this.name = name; } } const member = new Person('John'); console.log(typeof member);
Anonymous Poll
28%
A: "class"
28%
B: "function"
28%
C: "object"
17%
D: "string"
DevScrolls
90. What's the output?
class Person { constructor(name) { this.name = name; } } const member = new Person('John'); console.log(typeof member);
class Person { constructor(name) { this.name = name; } } const member = new Person('John'); console.log(typeof member);
Answer: C
Classes are syntactical sugar for function constructors. The equivalent of the Person class as a function constructor would be:
Calling a function constructor with new results in the creation of an instance of Person, typeof keyword returns "object" for an instance. typeof member returns "object".
Classes are syntactical sugar for function constructors. The equivalent of the Person class as a function constructor would be:
function Person(name) {
this.name = name;
}Calling a function constructor with new results in the creation of an instance of Person, typeof keyword returns "object" for an instance. typeof member returns "object".
91. What's the output?
let newList = [1, 2, 3].push(4); console.log(newList.push(5));
let newList = [1, 2, 3].push(4); console.log(newList.push(5));
Anonymous Poll
50%
A: [1, 2, 3, 4, 5]
11%
B: [1, 2, 3, 5]
6%
C: [1, 2, 3, 4]
33%
D: Error
DevScrolls
91. What's the output?
let newList = [1, 2, 3].push(4); console.log(newList.push(5));
let newList = [1, 2, 3].push(4); console.log(newList.push(5));
Answer: D
The .push method returns the new length of the array, not the array itself! By setting newList equal to [1, 2, 3].push(4), we set newList equal to the new length of the array: 4.
Then, we try to use the .push method on newList. Since newList is the numerical value 4, we cannot use the .push method: a TypeError is thrown
The .push method returns the new length of the array, not the array itself! By setting newList equal to [1, 2, 3].push(4), we set newList equal to the new length of the array: 4.
Then, we try to use the .push method on newList. Since newList is the numerical value 4, we cannot use the .push method: a TypeError is thrown
92. What's the output?
function giveLydiaPizza() {
return 'Here is pizza!';
}
const giveLydiaChocolate = () => "Here's chocolate... now go hit the gym already.";
console.log(giveLydiaPizza.prototype);
console.log(giveLydiaChocolate.prototype);
DevScrolls
92. What's the output? function giveLydiaPizza() { return 'Here is pizza!'; } const giveLydiaChocolate = () => "Here's chocolate... now go hit the gym already."; console.log(giveLydiaPizza.prototype); console.log(giveLydiaChocolate.prototype);
DevScrolls
Output?
Answer: D
Regular functions, such as the giveLydiaPizza function, have a prototype property, which is an object (prototype object) with a constructor property. Arrow functions however, such as the giveLydiaChocolate function, do not have this prototype property. undefined gets returned when trying to access the prototype property using giveLydiaChocolate.prototype.
Regular functions, such as the giveLydiaPizza function, have a prototype property, which is an object (prototype object) with a constructor property. Arrow functions however, such as the giveLydiaChocolate function, do not have this prototype property. undefined gets returned when trying to access the prototype property using giveLydiaChocolate.prototype.
93. What's the output?
const person = { name: 'Lydia', age: 21, };
for (const [x, y] of Object.entries(person)) {
console.log(x, y);
}
DevScrolls
Output ?
Answer: A
Object.entries(person) returns an array of nested arrays, containing the keys and objects:
[ [ 'name', 'Lydia' ], [ 'age', 21 ] ]
Using the for-of loop, we can iterate over each element in the array, the subarrays in this case. We can destructure the subarrays instantly in the for-of loop, using const [x, y]. x is equal to the first element in the subarray, y is equal to the second element in the subarray.
The first subarray is [ "name", "Lydia" ], with x equal to "name", and y equal to "Lydia", which get logged. The second subarray is [ "age", 21 ], with x equal to "age", and y equal to 21, which get logged.
Object.entries(person) returns an array of nested arrays, containing the keys and objects:
[ [ 'name', 'Lydia' ], [ 'age', 21 ] ]
Using the for-of loop, we can iterate over each element in the array, the subarrays in this case. We can destructure the subarrays instantly in the for-of loop, using const [x, y]. x is equal to the first element in the subarray, y is equal to the second element in the subarray.
The first subarray is [ "name", "Lydia" ], with x equal to "name", and y equal to "Lydia", which get logged. The second subarray is [ "age", 21 ], with x equal to "age", and y equal to 21, which get logged.
94. What's the output?
function getItems(fruitList, ...args, favoriteFruit) {
return [...fruitList, ...args, favoriteFruit]
}
getItems(["banana", "apple"], "pear", "orange")
DevScrolls
94. What's the output? function getItems(fruitList, ...args, favoriteFruit) { return [...fruitList, ...args, favoriteFruit] } getItems(["banana", "apple"], "pear", "orange")
DevScrolls
Output ?
Answer: D
...args is a rest parameter. The rest parameter's value is an array containing all remaining arguments, and can only be the last parameter! In this example, the rest parameter was the second parameter. This is not possible, and will throw a syntax error.
function getItems(fruitList, favoriteFruit, ...args) {
return [...fruitList, ...args, favoriteFruit];
}
getItems(['banana', 'apple'], 'pear', 'orange');
The above example works. This returns the array [ 'banana', 'apple', 'orange', 'pear' ]
...args is a rest parameter. The rest parameter's value is an array containing all remaining arguments, and can only be the last parameter! In this example, the rest parameter was the second parameter. This is not possible, and will throw a syntax error.
function getItems(fruitList, favoriteFruit, ...args) {
return [...fruitList, ...args, favoriteFruit];
}
getItems(['banana', 'apple'], 'pear', 'orange');
The above example works. This returns the array [ 'banana', 'apple', 'orange', 'pear' ]
95. What's the output?
function nums(a, b) {
if (a > b)
console.log('a is bigger');
else
console.log('b is bigger');
return a + b;
}
console.log(nums(4, 2)); console.log(nums(1, 2));
DevScrolls
95. What's the output? function nums(a, b) { if (a > b) console.log('a is bigger'); else console.log('b is bigger'); return a + b; } console.log(nums(4, 2)); console.log(nums(1, 2));
DevScrolls
Output?
Answer: B
In JavaScript, we don't have to write the semicolon (;) explicitly, however the JavaScript engine still adds them after statements. This is called Automatic Semicolon Insertion. A statement can for example be variables, or keywords like throw, return, break, etc.
Here, we wrote a return statement, and another value a + b on a new line. However, since it's a new line, the engine doesn't know that it's actually the value that we wanted to return. Instead, it automatically added a semicolon after return. You could see this as:
This means that a + b is never reached, since a function stops running after the return keyword. If no value gets returned, like here, the function returns undefined. Note that there is no automatic insertion after if/else statements!
In JavaScript, we don't have to write the semicolon (;) explicitly, however the JavaScript engine still adds them after statements. This is called Automatic Semicolon Insertion. A statement can for example be variables, or keywords like throw, return, break, etc.
Here, we wrote a return statement, and another value a + b on a new line. However, since it's a new line, the engine doesn't know that it's actually the value that we wanted to return. Instead, it automatically added a semicolon after return. You could see this as:
return; a + b;This means that a + b is never reached, since a function stops running after the return keyword. If no value gets returned, like here, the function returns undefined. Note that there is no automatic insertion after if/else statements!