So we want to store data to be used in a script, what options do we have?
Storing simple values:
If the value is single, such as a string or a number, we can just assign it to a variable.
Storing complex values:
Now since this data is more complex, the variable pointer stays on one side of the memory ( the stack), and the data stays on the other side of memory ( the heap). The variable points to the data, it does not hold the data. We can actually have several variables pointing at the same data.
Storing scripts such as functionality:
Now if we want to store a program routine, such as to print or to calculate or to fetch some data, etc. Then we use functions. Like any other object, functions can be moved around and placed inside of other scripts. Functions are also great to isolate one script from another script. Moreover, functions are great to help cleaning up the trash in memory because once returned, a function clears itself out (this cleaning is browser dependent since every browser uses a different kind of broom).
And there you have it. If you want a more in depth explanation please read my eBook