DevScrolls
59. What's the output?
const numbers = [1, 2, 3, 4, 5]; const [y] = numbers; console.log(y);
const numbers = [1, 2, 3, 4, 5]; const [y] = numbers; console.log(y);
Answer: C
We can unpack values from arrays or properties from objects through destructuring. For example:

The value of a is now 1, and the value of b is now 2. What we actually did in the question, is:

This means that the value of y is equal to the first value in the array, which is the number 1. When we log y, 1 is returned.
We can unpack values from arrays or properties from objects through destructuring. For example:
[a, b] = [1, 2];
The value of a is now 1, and the value of b is now 2. What we actually did in the question, is:
[y] = [1, 2, 3, 4, 5];
This means that the value of y is equal to the first value in the array, which is the number 1. When we log y, 1 is returned.
60. What's the output?
const user = { name: 'Lydia', age: 21 }; const admin = { admin: true, ...user }; console.log(admin);
const user = { name: 'Lydia', age: 21 }; const admin = { admin: true, ...user }; console.log(admin);
Anonymous Poll
37%
A: { admin: true, user: { name: "Lydia", age: 21 } }
48%
B: { admin: true, name: "Lydia", age: 21 }
11%
C: { admin: true, user: ["Lydia", 21] }
4%
D: { admin: true }
DevScrolls
60. What's the output?
const user = { name: 'Lydia', age: 21 }; const admin = { admin: true, ...user }; console.log(admin);
const user = { name: 'Lydia', age: 21 }; const admin = { admin: true, ...user }; console.log(admin);
Answer: B
It's possible to combine objects using the spread operator .... It lets you create copies of the key/value pairs of one object, and add them to another object. In this case, we create copies of the user object, and add them to the admin object. The admin object now contains the copied key/value pairs, which results in { admin: true, name: "Lydia", age: 21 }.
It's possible to combine objects using the spread operator .... It lets you create copies of the key/value pairs of one object, and add them to another object. In this case, we create copies of the user object, and add them to the admin object. The admin object now contains the copied key/value pairs, which results in { admin: true, name: "Lydia", age: 21 }.
61. What's the output?
const person = { name: 'Lydia' }; Object.defineProperty(person, 'age', { value: 21 });
console.log(person);
console.log(Object.keys(person));
DevScrolls
61. What's the output? const person = { name: 'Lydia' }; Object.defineProperty(person, 'age', { value: 21 }); console.log(person); console.log(Object.keys(person));
DevScrolls
Output?
Answer: B
With the defineProperty method, we can add new properties to an object, or modify existing ones. When we add a property to an object using the defineProperty method, they are by default not enumerable. The Object.keys method returns all enumerable property names from an object, in this case only "name".
Properties added using the defineProperty method are immutable by default. You can override this behavior using the writable, configurable and enumerable properties. This way, the defineProperty method gives you a lot more control over the properties you're adding to an object.
With the defineProperty method, we can add new properties to an object, or modify existing ones. When we add a property to an object using the defineProperty method, they are by default not enumerable. The Object.keys method returns all enumerable property names from an object, in this case only "name".
Properties added using the defineProperty method are immutable by default. You can override this behavior using the writable, configurable and enumerable properties. This way, the defineProperty method gives you a lot more control over the properties you're adding to an object.
62. What's the output?
const settings = { username: 'lydiahallie', level: 19, health: 90, };
const data = JSON.stringify(settings, ['level', 'health']);
console.log(data);
DevScrolls
62. What's the output? const settings = { username: 'lydiahallie', level: 19, health: 90, }; const data = JSON.stringify(settings, ['level', 'health']); console.log(data);
DevScrolls
Output?
Answer: A
The second argument of JSON.stringify is the replacer. The replacer can either be a function or an array, and lets you control what and how the values should be stringified.
If the replacer is an array, only the property names included in the array will be added to the JSON string. In this case, only the properties with the names "level" and "health" are included, "username" is excluded. data is now equal to "{"level":19, "health":90}".
If the replacer is a function, this function gets called on every property in the object you're stringifying. The value returned from this function will be the value of the property when it's added to the JSON string. If the value is undefined, this property is excluded from the JSON string.
The second argument of JSON.stringify is the replacer. The replacer can either be a function or an array, and lets you control what and how the values should be stringified.
If the replacer is an array, only the property names included in the array will be added to the JSON string. In this case, only the properties with the names "level" and "health" are included, "username" is excluded. data is now equal to "{"level":19, "health":90}".
If the replacer is a function, this function gets called on every property in the object you're stringifying. The value returned from this function will be the value of the property when it's added to the JSON string. If the value is undefined, this property is excluded from the JSON string.
63. What's the output?
let num = 10;
const increaseNumber = () => num++;
const increasePassedNumber = number => number++;
const num1 = increaseNumber();
const num2 = increasePassedNumber(num1);
console.log(num1); console.log(num2);
DevScrolls
Output?
Answer: A
The unary operator ++ first returns the value of the operand, then increments the value of the operand. The value of num1 is 10, since the increaseNumber function first returns the value of num, which is 10, and only increments the value of num afterwards.
num2 is 10, since we passed num1 to the increasePassedNumber. number is equal to 10(the value of num1. Again, the unary operator ++ first returns the value of the operand, then increments the value of the operand. The value of number is 10, so num2 is equal to 10.
The unary operator ++ first returns the value of the operand, then increments the value of the operand. The value of num1 is 10, since the increaseNumber function first returns the value of num, which is 10, and only increments the value of num afterwards.
num2 is 10, since we passed num1 to the increasePassedNumber. number is equal to 10(the value of num1. Again, the unary operator ++ first returns the value of the operand, then increments the value of the operand. The value of number is 10, so num2 is equal to 10.
64. What's the output?
const value = { number: 10 };
const multiply = (x = { ...value }) => {
console.log((x.number *= 2));
};
multiply();
multiply(); multiply(value); multiply(value);
DevScrolls
64. What's the output? const value = { number: 10 }; const multiply = (x = { ...value }) => { console.log((x.number *= 2)); }; multiply(); multiply(); multiply(value); multiply(value);
Output?
Anonymous Poll
33%
A: 20, 40, 80, 160
0%
B: 20, 40, 20, 40
50%
C: 20, 20, 20, 40
17%
D: NaN, NaN, 20, 40
DevScrolls
Output?
Answer: C
In ES6, we can initialize parameters with a default value. The value of the parameter will be the default value, if no other value has been passed to the function, or if the value of the parameter is "undefined". In this case, we spread the properties of the value object into a new object, so x has the default value of { number: 10 }.
The default argument is evaluated at call time! Every time we call the function, a new object is created. We invoke the multiply function the first two times without passing a value: x has the default value of { number: 10 }. We then log the multiplied value of that number, which is 20.
The third time we invoke multiply, we do pass an argument: the object called value. The *= operator is actually shorthand for x.number = x.number * 2: we modify the value of x.number, and log the multiplied value 20.
The fourth time, we pass the value object again. x.number was previously modified to 20, so x.number *= 2 logs 40.
In ES6, we can initialize parameters with a default value. The value of the parameter will be the default value, if no other value has been passed to the function, or if the value of the parameter is "undefined". In this case, we spread the properties of the value object into a new object, so x has the default value of { number: 10 }.
The default argument is evaluated at call time! Every time we call the function, a new object is created. We invoke the multiply function the first two times without passing a value: x has the default value of { number: 10 }. We then log the multiplied value of that number, which is 20.
The third time we invoke multiply, we do pass an argument: the object called value. The *= operator is actually shorthand for x.number = x.number * 2: we modify the value of x.number, and log the multiplied value 20.
The fourth time, we pass the value object again. x.number was previously modified to 20, so x.number *= 2 logs 40.
65. What's the output?
[1, 2, 3, 4].reduce((x, y) => console.log(x, y));
[1, 2, 3, 4].reduce((x, y) => console.log(x, y));
Anonymous Poll
13%
A: 1 2 and 3 3 and 6 4
19%
B: 1 2 and 2 3 and 3 4
19%
C: 1 undefined and 2 undefined and 3 undefined and 4 undefined
50%
D: 1 2 and undefined 3 and undefined 4