Suggest an editImprove this articleRefine the answer for “What does the on() method do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)`.on(eventName, listener)` registers a handler for a given event - every time the event fires via `.emit()`, all handlers added through `.on()` are called in registration order. **Key point:** `on()` is simply an alias for `addListener()` (`EventEmitter.prototype.on === EventEmitter.prototype.addListener`), there's no difference in behavior, and `.on()` is the modern, recommended choice.Shown above the full answer for quick recall.Answer (EN)Image## 1. What `on()` does > `.on(eventName, listener)` registers a handler (function) for a given event. > Every time an event named `eventName` fires via `.emit()`, > every handler added through `.on()` is called in registration order. ### Example: ```javascript const EventEmitter = require('events'); const emitter = new EventEmitter(); // Subscribe emitter.on('message', (text) => { console.log('Message received:', text); }); // Fire the event emitter.emit('message', 'Hello, Node.js!'); ``` Output: ```javascript Message received: Hello, Node.js! ``` ## 2. Syntax ```javascript emitter.on(eventName, listener) ``` - `eventName`, a string, the event's name (e.g. `'data'`, `'error'`, `'end'`). - `listener`, the handler function that gets called when the event occurs. ## 3. Example with several listeners ```javascript emitter.on('update', () => console.log('First listener')); emitter.on('update', () => console.log('Second listener')); emitter.emit('update'); ``` Output: ```javascript First listener Second listener ``` ## 4. How `on()` differs from `addListener()` | Method | Difference | |---|---| | `.on()` | The modern, more readable method name | | `.addListener()` | The old name, kept for compatibility (works exactly the same way) | In fact, `on()` **is an alias (synonym)** for `addListener()`: ```javascript EventEmitter.prototype.on === EventEmitter.prototype.addListener // true ``` That is: ```javascript emitter.on('event', listener); ``` and ```javascript emitter.addListener('event', listener); ``` do **exactly the same thing**. ## 5. Which one to use, and when - Use `.on()`, it's the modern, familiar choice (used throughout the official docs and every example). - `.addListener()` remains for backward compatibility with earlier Node.js versions. ## Summary > `on()` subscribes a function to an event (called on every `emit()`). > `addListener()` is a completely identical method, just an older name. > There is **no difference in behavior at all**, they're aliases for the same method. > > `.on()` is recommended as the modern standard.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.