When writing concise code it is important to remember the simple return statement. The return statement is a staple in many languages; it is important to understand how it works.
How It Works
As you can see, we can chain the upgradeValue method together, because we’re returning “this”. At that moment, it returns the newly created object; by returning the object, we have access to the functions we defined like upgradeValue and currentValue. The key here is to use the dot operator (.); the dot operator often allows us to access methods, but it really depends entirely on the return type. For example, if you returned an array, you could use square brackets () to access the elements in the array. Understanding what each method returns allows you to write more robust code with fewer variables.