Bird
0
0

What will be logged by the worker thread in this code?

medium📝 component behavior Q4 of 15
Node.js - Worker Threads
What will be logged by the worker thread in this code?
import { workerData } from 'worker_threads';
console.log(workerData.message);

And the main thread creates the worker as:
new Worker('./worker.js', { workerData: { message: 'Hello' } });
AError: workerData is not defined
BHello
Cundefined
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Understand workerData usage

    The workerData object contains the passed data, so workerData.message will be 'Hello'.
  2. Step 2: Confirm no errors

    Since workerData is imported correctly, no error occurs and the message prints as expected.
  3. Final Answer:

    Hello -> Option B
  4. Quick Check:

    workerData.message logs passed string [OK]
Quick Trick: workerData holds passed data, access properties directly [OK]
Common Mistakes:
  • Not importing workerData in worker
  • Expecting undefined if property exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes