Javascript: How to create chainable functions

What is the need to chain a function?

const finalResult = function1(
function2(
function3()
)
);

OR

const result3 = function3();
const result2 = function2(result3);
const finalResult = function1(result2);
var arr = [10, 20, 33, 45, 56, 60, 70, 46, 23, 34, 98, 67];

var doubleArr = arr.filter(val => val > 50).map(val => val*2);
// (5) [112, 120, 140, 196, 134]

This — makes chainable functions possible

function function1() {
console.log("i am function 1");
return this;
}

function function2() {
console.log("i am function 2");
return this;
}

function function3() {
console.log("i am function 3");
return this;
}

function1().function2().function3();
class Roots {
constructor() {
this.value;
}

data(val) {
this.value = val;
return this;
}

sq() {
var val = this.value
this.value = val*val;
return this;
}

cube() {
var val = this.value
this.value = val*val*val;
return this;
}

sqRoot() {
this.value = Math.sqrt(this.value);
return this;
}
}

var calculate = new Roots();
var result = calculate.data(2).sq().cube().sqRoot();
// result = { value: 8 }

CODER | BLOGGER | ARTIST | GHOST