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!
96. What's the output?
class Person {
constructor() {
this.name = 'Lydia';
}
}
Person = class AnotherPerson {
constructor() {
this.name = 'Sarah';
}
};
const member = new Person(); console.log(member.name);
DevScrolls
96. What's the output? class Person { constructor() { this.name = 'Lydia'; } } Person = class AnotherPerson { constructor() { this.name = 'Sarah'; } }; const member = new Person(); console.log(member.name);
Output ?
Anonymous Poll
0%
A: "Lydia"
40%
B: "Sarah"
20%
C: Error: cannot redeclare Person
40%
D: SyntaxError
DevScrolls
Output ?
Answer: B
We can set classes equal to other classes/function constructors. In this case, we set Person equal to AnotherPerson. The name on this constructor is Sarah, so the name property on the new Person instance member is "Sarah".
We can set classes equal to other classes/function constructors. In this case, we set Person equal to AnotherPerson. The name on this constructor is Sarah, so the name property on the new Person instance member is "Sarah".
97. What's the output?
const info = { [Symbol('a')]: 'b', }; console.log(info); console.log(Object.keys(info));
const info = { [Symbol('a')]: 'b', }; console.log(info); console.log(Object.keys(info));
Anonymous Poll
39%
A: {Symbol('a'): 'b'} and ["{Symbol('a')"]
39%
B: {} and []
6%
C: { a: "b" } and ["a"]
17%
D: {Symbol('a'): 'b'} and []
DevScrolls
97. What's the output?
const info = { [Symbol('a')]: 'b', }; console.log(info); console.log(Object.keys(info));
const info = { [Symbol('a')]: 'b', }; console.log(info); console.log(Object.keys(info));
Answer: D
A Symbol is not enumerable. The Object.keys method returns all enumerable key properties on an object. The Symbol won't be visible, and an empty array is returned. When logging the entire object, all properties will be visible, even non-enumerable ones.
This is one of the many qualities of a symbol: besides representing an entirely unique value (which prevents accidental name collision on objects, for example when working with 2 libraries that want to add properties to the same object), you can also "hide" properties on objects this way (although not entirely. You can still access symbols using the Object.getOwnPropertySymbols() method).
A Symbol is not enumerable. The Object.keys method returns all enumerable key properties on an object. The Symbol won't be visible, and an empty array is returned. When logging the entire object, all properties will be visible, even non-enumerable ones.
This is one of the many qualities of a symbol: besides representing an entirely unique value (which prevents accidental name collision on objects, for example when working with 2 libraries that want to add properties to the same object), you can also "hide" properties on objects this way (although not entirely. You can still access symbols using the Object.getOwnPropertySymbols() method).
98. What's the output?
const getList = ([x, ...y]) => [x, y]
const getUser = user => { name: user.name, age: user.age }
const list = [1, 2, 3, 4]
const user = { name: "Lydia", age: 21 }
console.log(getList(list)) console.log(getUser(user))
DevScrolls
98. What's the output? const getList = ([x, ...y]) => [x, y] const getUser = user => { name: user.name, age: user.age } const list = [1, 2, 3, 4] const user = { name: "Lydia", age: 21 } console.log(getList(list)) console.log(getUser(user))
Hey folks from now on you’ll receive frontend related update and new tips and tricks for your development