InterviewBuddy
All interview questions

Why should Express handlers return after sending an early response?

Intermediate

Answer

Sending a response does not stop JavaScript execution. Returning prevents later logic from running and attempting another response or unwanted side effect.

Explanation

Use an explicit return for validation and authorization failures.

Example

if (!valid) return res.status(400).json({ error: 'Invalid input' });