Facebook Pixel
JavaScript Interview: Output Prediction Questions

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

The answer

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); }.

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?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

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

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.