Variables

You already know that a variable is used to store some data that is to be used at some point in the program. Variables are one of the most important tools in a programming language. A variable really can vary, which is why it is so useful. When you play a video game (such as Mario Bros. 3), your score is saved to a variable. When you start a new game, the variable is set to 0, and each time you score points, the value of the variable increases (see Listing 2.9).

Listing 2.9 Keeping Score with Variables

var score = 0;// Increase the score by the amount given in pointsfunction increaseScore(points) { score = score + points;}

The player’s score is stored in a variable called score. In JavaScript, you use ...

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.