Chaining Methods In Javascript

Lets say I call any of the functions below with the argument “6.1.9”. Why am I able to write a method and chain an array index to the back of it? I understand that the split function turns the string into an array but I’m wondering about the general functionality of chaining. What other things can i chain onto methods? If possible i’d appreciate a link to a tutorial or documentation related to this functionality. Thanks.

function retrieveMajor(semver) {
	return semver.split(".")[0];

function retrieveMinor(semver) {
	return semver.split(".")[1];

function retrievePatch(semver) {
	return semver.split(".")[2];

There’s your answer.

It’s helpful think about it in terms of what a function/method returns when called. If it returns an array, you can tack an array index after it. If it returns a string, you can follow it with string properties or functions:


If it returns another function, you can chain function calls!

function addTo(n) {
  return function(x) {
    return n + x;
const someSum = addTo(7)(5); // 12
1 Like

This has to do with how program resolves functions and variables.

When encountered a function, the program has to evaluate the entirety of the statement and breakdown it down to their primitive type.

This is what the computer sees:

semver = String Object "6.1.9"
. = Calling String Object split Method

semver.split(".") needs to be resolve to a recongizable type.
["6", "1", "9"] = Array Object

["6", "1", "9"][0] = "6"

return "6"
1 Like