I'm struggling to grasp the concept of chaining and return in JavaScript. For example, consider the following code snippet:
let array = [1, 2, 3]
let newArray = array.map(val => val * 10).map(val => val * 10)
console.log(newArray) // [100, 200, 300]
It's fascinating how we can continue adding .map methods one after the other and it will keep returning a new array. It's like the chain of methods knows exactly when to stop and return a value. How does this behavior work under the hood, and more importantly, how can I implement similar functionality in my own code?