Java Reference
In-Depth Information
summarY
In this chapter you have built up knowledge of the fundamentals of JavaScript's data types and
variables and how to use them in operations. In particular, you saw that:
JavaScript supports a number of types of data, such as numbers, text, and booleans.
Text is represented by strings of characters and is surrounded by quotes. You must match the
quotes surrounding strings. Escape characters enable you to include characters in your string
that cannot be typed.
Variables are JavaScript's means of storing data, such as numbers and text, in memory so
that they can be used again and again in your code.
Variable names must not include certain illegal characters, like the percent sign ( % ) and the
ampersand ( & ), or be a reserved word, like with .
Before you can give a value to a variable, you must declare its existence to the JavaScript
interpreter.
JavaScript has the four basic math operators, represented by the symbols plus (+), minus ( ),
star ( * ), and forward slash ( / ). To assign values of a calculation to a variable, you use the
equals sign (=), termed the assignment operator.
Operators have different levels of precedence, so multiplication and division will be calcu-
lated before addition and subtraction.
Strings can be joined, or concatenated, to produce one big string by means of the + operator.
When numbers and strings are concatenated with the + operator, JavaScript automatically
converts the number into a string.
Although JavaScript's automatic data conversion suits us most of the time, on some
occasions you need to force the conversion of data. You saw how parseInt() and
parseFloat() can be used to convert strings to numbers. Attempting to convert strings that
won't convert will result in NaN (Not a Number) being returned.
Arrays are a special type of variable that can hold more than one piece of data. The data is
inserted and accessed by means of a unique index number.
exerCises
You can find suggested solutions to these questions in Appendix A.
1.
Write a JavaScript program to convert degrees centigrade into degrees Fahrenheit, and to
write the result to the page in a descriptive sentence. The JavaScript equation for Fahrenheit to
centigrade is as follows:
degFahren = 9 / 5 * degCent + 32
 
 
Search WWH ::




Custom Search