Facebook Pixel

What is the output of const obj = { x: 10, foo: () => console.log(this.x) }; obj.foo()?

undefined. Arrow functions inherit this from their lexical scope, not from the object. The enclosing scope's this does not have x. Use a regular function for methods: foo() { console.log(this.x); }.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in JavaScript Interview: Output Prediction Questions

1, 4, 3, 2. Sync code (1, 4) runs first. Then microtasks (3, Promise.then). Then macrotasks (2, setTimeout). This is the fundamental event loop order.

3, 3, 3. var is function-scoped; all callbacks share one i, which is 3 by the time they run. Use let to fix (logs 0, 1, 2).

1, 3, 2. Code before await runs synchronously (1). await pauses the function and schedules the rest as a microtask. Code after the foo() call runs (3). Then the rest of foo runs (2, microtask).

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.