Java Reference
In-Depth Information
JavaScript, on the other hand, is a weakly typed language and a lot more forgiving about how you
use different types of data. When you deal with data, you often don't need to specify type; JavaScript
will work that out for itself. Furthermore, when you are using different types of data at the same time,
JavaScript will work out behind the scenes what it is you're trying to do.
Given how easygoing JavaScript is about data, why talk about data types at all? Why not just cut to the
chase and start using data without worrying about their type?
First of all, while JavaScript is very good at working out what data it's dealing with, there are occasions
when it'll get things wrong or at least not do what you want it to do. In these situations, you need to
make it explicit to JavaScript what sort of data type you intended and how it should be used. To do that,
you fi rst need to know a little bit about data types.
A second reason is that data types enable you to use data effectively in your code. The things that can
be done with data and the results you'll get depend on the type of data being used, even if you don't
specify explicitly what type it is. For example, although trying to multiply two numbers together makes
sense, doing the same thing with text doesn't. Also, the result of adding numbers is very different from
the result of adding text. With numbers you get the sum, but with text you get one big piece of text con-
sisting of the other pieces joined together.
Let's take a brief look at some of the more commonly used data types: numerical, text, and Boolean. You
will see how to use them later in the chapter.
Numerical Data
Numerical data come in two forms:
Whole numbers, such as 145, which are also known as
integers . These numbers can be positive
or negative and can span a very wide range in JavaScript: -2 53 to 2 53 .
Fractional numbers, such as 1.234, which are also known as
fl oating-point numbers. Like integers,
they can be positive or negative, and they also have a massive range.
In simple terms, unless you're writing specialized scientifi c applications, you're not going to face problems
with the size of numbers available in JavaScript. Also, although you can treat integers and fl oating-point
numbers differently when it comes to storing them, JavaScript actually treats them both as fl oating-
point numbers. It kindly hides the detail from you so you generally don't need to worry about it. One
exception is when you want an integer but you have a fl oating-point number, in which case you'll round
the number to make it an integer. You'll take a look at rounding numbers later in this chapter.
Text Data
Another term for one or more characters of text is a string . You tell JavaScript that text is to be treated
as text and not as code simply by enclosing it inside quote marks ( ). For example, “Hello World” and
“A” are examples of strings that JavaScript will recognize. You can also use the single quote marks ( ' ),
so 'Hello World' and 'A' are also examples of strings that JavaScript will recognize. However, you
must end the string with the same quote mark that you started it with. Therefore, “A' is not a valid
JavaScript string, and neither is 'Hello World” .
Search WWH ::




Custom Search