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];
}
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: