saving and loading to text file

I’ve searched around and found a few examples but not enough to create a full understanding on how this works. What I’m trying to accomplish is upon pressing a button ingame all the variables “using javascript” are saved to the text document. I’m not sure how to write each variable to a text file and then load the text file every time the game is Reopened again. So how exactly is each variable listed in a text file and how does it know where to save and load said variables ?