Skip to content

Chaining method calls in JavaScript

In JavaScript sometimes we can chain method calls, like this:


It's pretty convenient to do so.

Instead of writing


we can simplify in a one-liner.

This is possible if each method returns the object itself. In other words, the implementation must be something like this:

const car = {
  start: function() {
    return this
  drive: function() {
    return this

It's important to note that you can't use arrow functions, because this in an arrow function used as object method is not bound to the object instance.

I like to use arrow functions all the time, and this is one of the cases where you can't.

Chained method calls are great when you are not returning a set of values from the method, otherwise you obviously need to assign a method call to a variable, and chaining is not possible:

const result = car.start()
if (result) {
→ Download my free JavaScript Handbook!



You might be interested in those things I do:

  • Learn to code in THE VALLEY OF CODE, your your web development manual
  • Find a ton of Web Development projects to learn modern tech stacks in practice in THE VALLEY OF CODE PRO
  • I wrote 16 books for beginner software developers, DOWNLOAD THEM NOW
  • Every year I organize a hands-on cohort course coding BOOTCAMP to teach you how to build a complex, modern Web Application in practice (next edition February-March-April-May 2024)
  • Learn how to start a solopreneur business on the Internet with SOLO LAB (next edition in 2024)
  • Find me on X

Related posts that talk about js: