🟢 Node.js Intermediate

What is the difference between PUT and PATCH in REST APIs?

Why Interviewers Ask This

Mid-level Node.js roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.

Answer

Both PUT and PATCH HTTP methods update resources, but with different semantics. PUT is a full replacement — the request body must contain the complete resource representation. Any fields not included are set to null/default. PUT /users/1 { name: "Alice", email: "a@b.com", age: 30 } replaces the entire user record. If only name is sent, email and age become null. PUT must be idempotent — making the same request multiple times produces the same result. PATCH is a partial update — only the fields included in the request body are updated; others remain unchanged. PATCH /users/1 { name: "Alice" } only updates the name field. PATCH is more efficient for updating a single field in a large document. In Express: handle both with separate route handlers, with PATCH using spread/merge logic: const updated = { ...existing, ...req.body };. Most practical REST APIs use PATCH for partial updates and PUT for full replacements, though many APIs use PATCH exclusively for simplicity.

Pro Tip

This topic has Node.js-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.