Method Chaining in JavaScript

By joyal_easycodelearner | 05 Jul 2026 | Topic: Learn

Method chaining in JS means calling one function or method one after in one continuous line of code.


Why do we use this :


  1. In order to avoid multiple lines of code we can use to get the results in one line.

For example think about a scenario where you ask the username from the prompt and you want to capitalise first character and rest of them in small letters ,and also there might be some white spaces as well in this case we normally might use multiple lines of code


  1. to trim the whitespaces ,
  2. then capitalize,
  3. then smallcases...etc

but if you are familiar with the concept of method chaining then we can solve this multiple lines of code into one single code and achieve the results


Try the following code:


let userName=window.prompt("Enter the username");
userName=userName.trim().charAt(0).toUpperCase()+userName.slice(1).toLowerCase();

← Back to Topic