Java Reference
In-Depth Information
tYpes of data in JavasCript
Data can come in many different forms, or types . You'll recognize some of the data types that
JavaScript handles from the world outside of programming—for example, numbers and text. Other
data types are a little more abstract and are used to make programming easier; one example is the
object data type, which you won't see in detail until Chapter 5.
Some programming languages are strongly typed. In these languages, whenever you use a piece of
data, you need to explicitly state what sort of data you are dealing with, and use of that data must
follow strict rules applicable to its type. For example, in a strongly typed language you can't add a
number and a word.
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 its type?
First of all, although JavaScript is very good at working out what data it's dealing with, on occasion
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 first 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
you can do with data and the results you'll get depend on the type of data being used, even if you
don't explicitly specify what type it is. For example, although trying to multiply two numbers 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 consisting 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 see how to use them later in the chapter.
numerical data
Numerical data comes 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 floating‐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 scientific applications, you're not going to face
problems with the size of numbers available in JavaScript. Also, although you can treat integers and
floating‐point numbers differently when it comes to storing them, JavaScript actually treats them both
as floating‐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 floating‐point number, in which case you'll
round the number to make it an integer. You take a look at rounding numbers later in this chapter.
 
Search WWH ::




Custom Search