Errata

Learning JavaScript

Errata for Learning JavaScript

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released. If the error was corrected in a later version or reprint the date of the correction will be displayed in the column titled "Date Corrected".

The following errata were submitted by our customers and approved as valid errors by the author or editor.

Color key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted By Date submitted Date corrected
Other Digital Version
34
middle

The text reads:

"In addition to converting strings to numbers, you can also test the value of a variable to see whether it's infinity through the IsFinite function."

First: the ECMAScript function in question is isFinite(), not IsFinite().

Besides, it is not clear what infinity signifies. isFinite(Infinity) evaluates to false, and so does isFinite("infinity"), as JavaScript implicitly converts the string "infinity" to the ECMAScript number property Infinity. isFinite(infinity), conversely, does not evaluate to anything, not even to null, as infinity (without quotes) is not a valid ECMAScript expression unless infinity is declared as a variable (say var infinity = 99;). Declaring infinity as a variable is not recommended, as Infinity and -Infinity (with uppercase i) are number properties in ECMAScript signifying an infinite number (positive infinity and negative infinity respectively).

Note from the Author or Editor:
Change IsFinite to isFinite on page 34.

Troels Vedel Kloejgaard  Jul 18, 2012 
Printed, PDF, Other Digital Version
Page 136
4th paragraph, 3rd sentence

Really trivial one this time: "Most browsers supportDOM Leve 2...." needs a space in "supportDOM".

Note from the Author or Editor:
Add space between support and DOM in sentence 3, paragraph 4.

Should be

support DOM

Not

supportDOM

J. Randall Owens  Apr 23, 2012 
Printed, PDF, Other Digital Version
Page 92
example line after 4th text paragraph, just before "The Math Properties"

Defines newValue = Math.SQRT1; which should probably be either Math.sqrt(1); or Math.SQRT2; or Math.SQRT1_2;

In that context, any valid Math property or method will do, but SQRT1 is neither.

Note from the Author or Editor:
Page 92, last code snippet, change from

Math.SQRT1;

To

Math.sqrt(4);

J. Randall Owens  Apr 17, 2012 
Printed, PDF, Other Digital Version
Page 70
2nd paragraph

Speaking of a variable, it says it "is a string primitive variable, true." "true" is formatted as though it's the boolean value in constant width, but in context, it seems that the common English word is what's meant here.

Note from the Author or Editor:
Removing literal formatting from the word, true, in page 70, second paragraph, second sentence.

J. Randall Owens  Apr 12, 2012 
PDF, Other Digital Version
Page 41
8th Paragraph

***The paragraph and accompanying code snippet exists as follows:

However, with the following, where variables are lined up with commas between them, the first vairable is set and the second one ends up undefined:

var nValue1,nValue2 = 3; // nValue2 is undefined


***The above should read as follows:

However, with the following, where variables are lined up with commas between them, the second vairable is set and the first one ends up undefined:

var nValue1,nValue2 = 3; // nValue1 is undefined

Note from the Author or Editor:
Page 41, change following line

However, with the following, where variables are lined up with commas between them, the first variable is set and the second ends up undefined:

to

However, with the following, where variables are lined up with commas between them, the first variable is undefined and only the second is set.

Leonard Peris  Mar 21, 2012 
Other Digital Version
166
2nd paragraph

For the section, Callback Functions, the book says, "Returning to the Array methods, the filter method ensures that elements* are not added to any element* unless they pass certain criteria" when I believe it should instead say, elements/array in place of elements/element.

Note from the Author or Editor:
Page 116, second paragraph, change first sentence to

Returning to the Array methods, the filter method ensures that elements are not added to an array unless they pass certain criteria

Roy Chu  Mar 16, 2012 
Printed
Page 299
3rd & 5th paragraphs

The third paragraph says: "I also used it with the methods in the first example, but not with the song array."

I think you meant "but not with the performedBy array." That is the name of the array declared in the Tune function.

The 5th paragraph says "The performers array, though, is not assigned to the object using this..."

I think you meant "The performedBy array, though...." I'm assuming you're referring to the same array mentioned in the 3rd paragraph.

Note from the Author or Editor:
On page 299, paragraph 3, change the sentence with

I also used it with the methods in the first example, but not with the song array.

To

I also used it with the methods in the first example, but not with the performedBy array.

On page 299, paragraph 5, change

The performers array, though, is not assigned to the object using this

To

The performedBy array, though, is not assigned to the object using this



Mike M  Feb 04, 2011 
Printed
Page 163
Quiz Question 7

Quiz question 7 says to use a document.writeln function call to output the key pressed, but the solution uses alert instead of document.writeln.

Note from the Author or Editor:
Change the following line in Answer 7

alert(theEvent.which);

To

document.writeln(theEvent.which);

Mike M  Feb 03, 2011 
Printed
Page 114
Example 5-5

The example includes the exclamation point in the call to the base method, which makes the supplied output incorrect. For example:

var newString = baseString("World!");

should be:

var newString = baseString("World");

Otherwise the output would actually be "Hello World!!" and not "Hello World!" as stated.

The source code is fixed in the downloaded example source code.

Note from the Author or Editor:
In Example 5-5, remove the exclamation point in the following lines of code:

var newString = baseString("World!");

var notherString = baseString("Reader!");

var lastString = anotherBase("you!");

Mike M  Feb 02, 2011 
Printed
Page 36
First code snippet (and neighboring paragraphs)

"You use the keyword const to create a JavasScript constant..."

Not in Internet Explorer you don't (syntax error), nor in any other browser that doesn't support JavaScript 1.5.

A quick test shows that at least current versions of Firefox and Chrome support it, but a majority of developers are probably interested in supporting IE...

Note from the Author or Editor:
Page 42, in section titled Constants: Named but Not Variables, add a note at end:

const is defined as a JavaScript keyword, but isn't supported in Internet Explorer at this time.

Kenneth G. Franqueiro  Jan 07, 2011 
Printed
Page 148
1st paragraph

The text states that the first event handler, assigned using the traditional model fires, while actually it is the inline model that fires first. The code works fine, but the text reverses the traditional/inline terminology.

Note from the Author or Editor:
Page 148, paragraph 1, change to:

The alert window with the message "hello there" pops up for the first event handler, assigned using the inline model. However, the second message for the event handler assigned using the traditional model does not display.

Anonymous  Oct 02, 2010 
Printed
Page 22
near bottom, variable names example

The variable (or function) name
validate-name
is illegal because it contains a hyphen.
The preceding text shouldn't mention 'dashes' either.

Note from the Author or Editor:
Change paragraph 9, page 22 to:

The CamelCase naming format makes the variable much more readable, though underscores between the variable "words" work as well:

Drop the following from the code snippet that follows:

validate-name

Alan Rew  Aug 19, 2010 
Printed
Page 63
text

The text says:

"When the variable's value satisfies the condition, the loop is finished:"

This should say:

"When the variable's value does not satisfy the condition, the loop is finished:"

Note from the Author or Editor:
Change last sentence of paragraph 4 on page 63 to:

When the value causes the conditional test to fail, the loop is exited.

Anonymous  Aug 17, 2010 
Printed
Page 35
at the top of the page

if (undefinedStrint) at the top of the page should be:

if (undefString)

to be consistent with the variable declaration on the previous page.

Note from the Author or Editor:
change undefinedString in code example at top of page 35 to undefString

Anonymous  Aug 09, 2010 
97
Center of the page

For this code:

var newFruit = fruitArray.slice(2,4); // returns an array of 3 elements: melon,
banana, and lime

The slice function actually returns 2 elements because it stops before reaching 4.

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/slice

Note from the Author or Editor:
Change code sample in page 97 from:

var newFruit = fruitArray.slice(2,4); // returns an array of 3 elements: melon,banana, and lime

To

var newFruit = fruitArray.slice(2,4); // returns an array of 2 elements: melon, banana

David Friedman  Jun 23, 2010 
Printed
Page 37
Answer 5.

The question is "How would you determine whether it's null or undefined?".

The answer given is "Passing a variable that's not declared or defined to a function or object method results in a Javascript error...". This answer does not exactly match the question. Note that undeclared is different from undefined and is different from null. That statement is true if variable passed in is undeclared, e.g.

function foo(a) { alert(a); }
foo(b); // this will cause a Javascript error

It is not true if variable is simply undefined.

function foo(a) { alert(a); }
var b; // this is undefined
foo(b); // no errors here

Note from the Author or Editor:
Change Question 5's answer on Page 37 to:

Use the following to test whether the variable has been assigned a value (not null and not undefined):

if (a) { ... }

If the value has never been declared, though, this test results in an error, as does passing the non-existent variable to the function. The way to prevent the error is to test the value before you pass it to the function, using the following:

if (typof(a) != "undefined") { ... }

Combined with the test for null above, you can test that a variable exists, is defined, and actually has a non-null value with:

if ((typeof(a) != "undefined") && a) {
...
}

Herbie  Feb 21, 2010 
Printed
Page 93
2nd paragraph under "The Math Methods" section

At the end of the second paragraph under "The Math Methods", it says:

"The variable pVal is set to 3.4 in the following snippet:"

It should say:

"The variable pVal is set to 3.45 in the following snippet:"

Note from the Author or Editor:
Change sentence on page 93 just before first code snippet in Math Methods section to:

The variable pVal is set to 3.45 in the following snippet

Scott Marcus  Feb 11, 2010 
Printed
Page 73
Last paragraph

After covering 5 properties of the Number object, the last paragraph goes on to say:

"I use the Number object in the listing of the four properties to highlight...".

It should say:

"I use the Number object in the listing of the five properties to highlight...".

Note from the Author or Editor:
Change first sentence of last paragraph on page 73 to:

"I use the Number object in the listing of the five properties to highlight...".

Scott Marcus  Feb 09, 2010 
Other Digital Version
183
examlpe 7-7

the code snippet reads:

function setup(evnt) {
var evtObject = document.getElementById("clickme");
// test for object model
if (evtObject.addEventListener) {

document.addEventListener("click",clickMe,false); <<------

} else if (evtObject.attachEvent) {
evtObject.attachEvent("onclick", clickMe);
} else if (evtObject.onclick) {
evtObject.onclick=clickMe;
}
}

it should reads:

...
evtObeject.addEventListener("click",clickMe,false); <<------
...

Note from the Author or Editor:
In Example 7-7, the line that reads:

document.addEventListener("click",clickMe,false);

Should be changed to:

evtObject.addEventListener("click",clickMe,false);

Carlos D  Jan 23, 2010 
Other Digital Version
189
7.4 Quiz

The exercise number 7 reads:

7. Write JavaScript to capture the keydown event on the document and print out the key pressed using a document.writeln function call.

It should read:

7. Write JavaScript to capture the keydown event on the document and print out the key pressed using a alert function call.

Note from the Author or Editor:
Change question 7 in page 163 (printed) to read:

7. Write JavaScript to capture the keydown event on the document and print out the key pressed using a alert function call.

carlos D  Jan 23, 2010 
Printed
Page 20-21
Table 2-1, Table 2-2

Three additional keywords, omitted from Table 2-1, are null, true, and false.

The reserved word public is listed twice in Table 2-2.

Note from the Author or Editor:
The reserved words null, true, and false should be added to Table 2-1 and the duplicate public should be removed from Table 2-2

Conrad Halling  Sep 02, 2009 
Printed
Page 20
Between 3rd and 4th paragraphs

var-ident should be var_ident

Note from the Author or Editor:
The variable name example var-ident should be var_ident at the top of page 20.

Conrad Halling  Sep 02, 2009 
Printed
Page 29
2nd and 3rd paragraphs, table 2.4

Although on page 28 the book discusses using the String() global function for converting a variable to a string, the top of page 29 suddenly refers to toString(). My experimentation with this function (which is actually a method) showed that String() and toString() do different things.

toString should be changed to String in paragraphs 2 and 3 and table 2-4 of page 29.

Note from the Author or Editor:
toString should be changed to String in paragraphs 2 and 3 and table 2-4 of page 29.

Conrad Halling  Sep 02, 2009 
Printed
Page 22
Last paragraph

"Though you can use a dollar sign, number, or underscore to begin a variable..."

should be

"Though you can use a dollar sign, letter, or underscore to begin a variable..."

Note from the Author or Editor:
Page 22, last paragraph, first line from

Though you can use a dollar sign, number, or underscore to begin a variable...

to

Though you can use a dollar sign, letter, or underscore to begin a variable...

Conrad Halling  Sep 02, 2009 
Printed
Page 7
Code sample at bottom of page

function hello(??) {

should be

function hello() {

Note from the Author or Editor:
Remove the ?? in the code sample at bottom of page 7

Conrad Halling  Sep 02, 2009 
Printed
Page 33
5th paragraph

In discussing the parseInt function, the example indicates that parseInt("266",16) results in a value of 550. Although my understanding of converting to hex and octal is rudimentary, my code indicate that the result is 614, not 550. I'm not sure if the difference between my result and that of the author is my mistake or not, but I ran the code verbatim, and my result is different.

Note from the Author or Editor:
The use of parseInt("266","16") shown in page 33, paragraph 5, results in a value of 614, not 550 as shown in the book.

Eric Brown  Aug 31, 2009 
3.2.5
binary flags example

The flags value is set to 0101, and the flag 'order' is apparently ABCD (A/C false, B/D true).

[quote]
flag A: false
flag B: true
flag C: false
flag D: true
[/quote]

>> C is false!

But then the flag order is reversed to DCBA:

[quote]
Each bit mask flag is then represented as follows:
var flag_A = 0x1; // 0001
var flag_B = 0x2; // 0010
var flag_C = 0x4; // 0100
var flag_D = 0x8; // 1000
[/quote]

>> C is true (suddenly)!

Note from the Author or Editor:
Page 46 in chapter 3, the code example from Mozilla that reads like:

flag A: false
flag B: true
flag C: false
flag D: true

should read:

flag A: true
flag B: false
flag C: true
flag D: false

The values were transposed

Anonymous  Aug 04, 2009 
3.2
6th example

The example reads:

var nValue1,nValue2 = 3; // nValue2 is undefined

But it should certainly read:

var nValue1,nValue2 = 3; // nValue1 is undefined

Note from the Author or Editor:
The current 6th example in Section 3.2 and the line of text preceding it currently read:

However, with the following, where variables are lined up with commas between them, the first variable is set and the second ends up undefined:

var nValue1,nValue2 = 3; // nValue2 is undefined

It should read:

However, with the following, where variables are lined up with commas between them, the second variable is set, and the first ends up undefined:

var nValue1,nValue2 = 3; // nValue1 is undefined

Anonymous  Aug 04, 2009 
?
Section 7.2.2

Just a trivial typo...

The text below example 7.4 says that the alert windows shown by clicking on the innermost div will be:

1. Select element event
2. First element event
3. Document event

However, according to the listing in example 7.4 it should say:

1. ***Second*** element event
2. First element event
3. Document event

Note from the Author or Editor:
The text before Example 7-4 should be corrected to read:

1. Second element event
2. First element event
3. Document event

Shane Bell  Jul 29, 2009 
Printed
Page 64
3rd paragraph

The 3rd paragraph states: "...Example 3-8 creates a custom object named MyTest...." but the custom object in code Example 3-8 is actually named MyText instead.

Note from the Author or Editor:
The object referred to as MyTest in sentence 3, paragraph 3, page 64, should be MyText.

May 01, 2009 
??
Example 8.5

In example 8.5 the line

<textarea name="text4" cols="50" rows="10">The text area</textarea>

should read

<textarea name="text4" id="text4" cols="50" rows="10">The text area</textarea><br /><br />

Note from the Author or Editor:
In Example 8-5, the line defining the textarea element should be changed to:

<textarea name="text4" id="text4" cols="50" rows="10">The text area</textarea>

Steven Greve  Mar 25, 2009 
2.6
5th code example in section 2.6

The code snippet reads:

if (sValue) // true if a variable is both defined and given a value (including empty string)

This is in error. An empty string evaluates to false, as shown in this code:

var someVar = "";
if (someVar) {
document.writeln("<p>someVar evaluates to TRUE</p>");
}
else {
document.writeln("<p>someVar evaluates to FALSE</p>");
}

Note from the Author or Editor:
The code snippet should read:

if (sValue) // true if a variable is both defined and given a value

Anonymous  Dec 12, 2008 
??
Section 13.1.1 code example below, marked with <<<----

The line marked below is not complete. Should it be competed with " val2);".

Number.prototype.add=function(val1,val2) { return val1 + val2; };
var num = new Number();
num.prototype.add = function(val1, <<<----
var sum = num.add(8,3); // sum is 11

Note from the Author or Editor:
The following line should be removed (it was left in by accident):

num.prototype.add = function(val1,

The actual example should read:

Number.prototype.add=function(val1,val2) { return val1 + val2; };
var num = new Number();
var sum = num.add(8,3); // sum is 11

Bill Judd  Dec 06, 2008