JavaScript Easy technical 1 views 1 min read

How do you implement method chaining in JavaScript?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of JavaScript conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

Method chaining is a pattern where multiple methods are called on the same object sequentially by returning this from each method.

     class Calculator {
       constructor(value = 0) {
         this.value = value;
       }

       add(num) {
         this.value += num;
         return this; // Enable chaining
       }

       subtract(num) {
         this.value -= num;
         return this;
       }

       multiply(num) {
         this.value *= num;
         return this;
       }

       divide(num) {
         this.value /= num;
         return this;
       }

       getResult() {
         return this.value;
       }
     }

     const result = new Calculator(10)
       .add(5)
       .multiply(2)
       .subtract(3)
       .getResult();

     console.log(result); // 27
     

Advanced pattern with error handling:

     class QueryBuilder {
       constructor() {
         this.query = '';
         this.params = [];
       }

       select(...fields) {
         this.query = `SELECT ${fields.join(', ')}`;
         return this;
       }

       from(table) {
         this.query += ` FROM ${table}`;
         return this;
       }

       where(condition, ...params) {
         this.query += ` WHERE ${condition}`;
         this.params.push(...params);
         return this;
       }

       build() {
         return { query: this.query, params: this.params };
       }
     }

     const query = new QueryBuilder()
       .select('id', 'name', 'email')
       .from('users')
       .where('age > ?', 18)
       .build();
     

Immutable chaining pattern:

     class ImmutableArray {
       constructor(arr = []) {
         this.arr = arr;
       }

       map(fn) {
         return new ImmutableArray(this.arr.map(fn));
       }

       filter(fn) {
         return new ImmutableArray(this.arr.filter(fn));
       }

       value() {
         return this.arr;
       }
     }

     const result = new ImmutableArray([1, 2, 3, 4])
       .map(x => x * 2)
       .filter(x => x > 4)
       .value(); // [6, 8]
     

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?