MCQ Hive stays free thanks to ads. Please disable your ad blocker for this site so content and features keep working.
Web Development
JavaScript
Beginner
Q
What does this log?
const obj = { name: 'PMD', show: () => console.log(this.name) };
obj.show();
APMD
BReferenceError
Cobj
Dundefined (in a browser module or script, this is not obj)
Correct Answer:
D. undefined (in a browser module or script, this is not obj)
Explanation:
Arrow functions take their this from the enclosing scope. Use a regular method, show() { ... }, to get obj.
ALSO KNOW (related points):
• With const obj = {a: 1};, what happens with obj.a = 2;? → It works; const prevents reassigning the variable, not mutating the object
• Which string is valid JSON? → {"name": "Karachi", "temp": 35}
• What does the spread copy const b = [...a]; do when a contains objects? → It makes a shallow copy: a new array, but the same object references inside it