Kindle Notes & Highlights
hypertext linking,
To create a line comment, you add
two slashes ( // ) before the text you want to use as a comment. The // characters instruct
JavaScript interpreters to ignore all text following the slashes on the same line. You can
place a line c...
This highlight has been truncated due to consecutive passage length restrictions.
You create a block comment by adding /* to the first line that
you want included in the block, and you close a comment block by typing */ after the last
character in the block. Any text or lines between the opening /* characters and the closing
*/ characters are ignored by JavaScript interpreters.
A JavaScript block comment uses the same syntax as ...
This highlight has been truncated due to consecutive passage length restrictions.
. Why should you end every JavaScript statement with a semicolon?
Even though the statements do not end in semicolons, the preceding script is legal. However, that’s not the end of the story. Programmers often adopt conventions in their code that make the code easier for themselves or other programmers to read in a text editor. In the case of semicolons, it is considered good JavaScript programming practice to end every statement with a semicolon. The semicolon serves to identify the end of each statement, making it easier for a programmer to read his or her own code (and for other programmers to read the code later on). In addition, a JavaScript parser must perform work on a script without semicolons to determine where each statement ends, which can slow down a web page and detract from the user experience. For these reasons, you should be sure to end all of your JavaScript statements with semicolons.
Identifiers must begin with an uppercase or lowercase ASCII letter, dollar sign ($), or
underscore ( _ ).
You can use numbers in an identifier but not as the first character.
You cannot include spaces in an identifier.
You cannot use reserved words for identifiers.
The equal sign in the preceding statement is called an assignment operator because it
assigns the value on the right side of the expression to the vari...
This highlight has been truncated due to consecutive passage length restrictions.
You are not required to use the var keyword to declare a variable. However, omission of the var
keyword affects where a variable can be used in a script. In addition, declaring variables without the
var keyword can make it more challenging later to identify where in a program a variable is first
declared. For these reasons, it is good prog...
This highlight has been truncated due to consecutive passage length restrictions.
Using the Array Object
Arrays are represented in JavaScript by the Array object. The Array object contains
a special constructor named Array() that provides another way to create an array.
A constructor is a special type of function that is used as the basis for creating reference
variables (that is, variables whose data type is the reference data type). You can create
new arrays with the Array() constructor by using the keyword new and the name of the
Array() constructor with the following syntax:
var arrayName = new Array(number of elements);
Within the parentheses of the
...more
Unlike getElementById(), which uses the singular version of “element,”
the word “elements” in getElementsByTagName() has an s at the
end. Be sure not to type getElementByTagName(), which is not a valid
method name.
Many programmers often name counter variables count, counter, or
something similar. The letters i, j, k, l, x, y, and z are also commonly used
as counter names. Using a name such as count, the letter i (for increment),
or a higher letter helps you remember (and lets other programmers know)
that the variable is being used as a counter.
As you can see in the syntax description, the statements execute before the conditional expression is evaluated. Unlike the simpler while statement, the statements in a do/while statement
always execute at least once, before the conditional expression is evaluated the first time.
You can omit any of the three parts of the for statement, but you must
include the semicolons that separate the sections. If you omit a section, be
sure that you include code within the command block that will end the for
statement or your program may get caught in an infinite loop.
Skills at Work
When designing a program that involves loops, especially a large and complex program, it can
be challenging to explain the structure of the program and the relationships between its parts to
other team members who might be working with you to create it. It’s common for programmers to
create a visual representation to illustrate the parts of a program and how they fit together, both
before and during development. For loops, such diagrams often take the form of a flow chart, which
shows program components as boxes of different shapes, with lines connecting those
...more
Recall that a falsy value is one of six values that are treated in comparison
operations as the Boolean value false, and that a truthy value is any value
other than the six falsy values.
Some developers add comments to their closing braces to indicate what
statement each brace ends. For instance, after the first closing brace in Step 2,
you might add the comment // end if to indicate that the brace closes the
preceding if statement. Especially when your code includes structures nested
several levels deep, such comments can make it easier to add or remove
levels of nesting.