This is to clarify Section 13.4 on page 264. The focus of that example is to eliminate a bunch of repetitive object references. Certainly, the with statement is nice since it alleviates the need to type in full object reference each time you need to reference a form's elements. Just supply reference to the form in the with statement then refer directly to the form elements in the body of the with statement. Both of the following examples make use of the with statement. However, the purpose of the examples is to clarify another issue. The subtle nature of this issue warrants some further discussion.

The book notes at the top of page 262 that value properties of form elements can't hold numbers. For example,


seemingly stores the number 17 into the value property. But, since value properties are inherently strings, the string "17" is actually stored. So you see, the statements in Section 13.4



result in the two numbers entered into the form being concatenated rather than added. Since the numbers returned by the parseFloat() function are stored back into value properties they are actually converted back into strings. This effectively "undoes" the parse job. Then the last statement concatenates two string values. The first button below calls the concatenating plus() function as shown in Section 13.4. The second button calls a function named add() which uses local variables to store the parsed values. In that case the parsing is not "undone." The function works as planned and adds the two numbers. Test both functions below and then have your browser display the source code for this page so that you can see the two functions. Besides demonstrating the subtlety about value properties, both functions make good use of the with statement.

Enter a number in each field: