In an event-drive program, the code is broken into pieces. Some runs when the page loads, some when different buttons are clicked.
Variables are how the different code fragments work together. When one piece of code changes a variable's value, the next piece of code will use that new value.
You'll see this in almost every example. To write the code for an event, start by working out how it changes variables. That's half the battle.
Here's some code from one of the examples.
The variable coffeeExpense
is the first thing inside the wrapper:
(function($) {
var coffeeExpense = 0;
...
}(jQuery));
All of the code fragments are inside the wrapper, so coffeeExpense
is available to every code fragment. They all use the variable.