Head First JavaScript Code: Chapter 2, Storing Data

Chapter 2 Opening Image

In the real world, people often overlook the importance of having a place to store all their stuff.

Head First JavaScript

Not so in JavaScript. You simply don't have the luxury of walk-in closets and three-car garages. In JavaScript, everything has its place, and it's your job to make sure of it. The issue is data—how to represent it, how to store it, and how to find it once you've put it somewhere. As a JavaScript storage specialist, you'll be able to take a cluttered room of JavaScript data and impose your will on it with a flurry of virtual labels and storage bins.

Download the Code

Code Description and Viewing

Donuts

The Donuts web page is built incrementally throughout the chapter. Each numbered HTML file corresponds to an incremental version of the web page. When a page number appears beside a file, it is either the page where the file's contents are presented or the exercise (solution) corresponding to the file. The Place Order button ONLY works if the page is opened from a real web server. The button WON'T work when the page is opened directly from your local hard disk because the PHP server script requires a web server. This doesn't limit the JavaScript code in any way.

  • donuts1.html - Donuts web page with NaN problem (page 59)
  • donuts2.html - Donuts web page with NaN problem solved thanks to constant initialization (page 62)
  • donuts3.html - Donuts web page with numeric strings converted to numbers (page 68)
  • donuts4.html - Donuts web page with improved placeOrder() function (page 74)
  • donuts5.html - Donuts web page with parseDonuts() function and improved updateOrder() function (page 79)
  • donuts.css - External CSS style sheet for the Donuts web page
  • donuts.php - PHP server script that processes donut orders for the Donuts web page
  • donuttime.png - Donut/clock image that appears on the Donuts web page

Attention Internet Explorer Users

Although we teach it and explain that it isn't supported across all browsers, the "const" keyword is used in the code throughout Chapter 2. It is supported in Firefox and Safari but not IE. In light of that, you should replace "const" with "var" in the example code so that it works in all browsers. Get the alternate code here.