Data can come in many different forms, or what we term types. You'll recognize some of the data types that JavaScript handles from the world outside programming, for example, numbers and text. Other data types are a little bit more abstract and are used to make programming easier; one example is the object data type, which we won't see in detail until Chapter 4.
Some programming languages are strongly typed languages. In these languages, whenever we use a piece of data we need to explicitly state what sort of data we are dealing with, and use of that data must follow strict rules applicable to its type. For example, we can't add a number and a word together.
JavaScript, on the other hand, is a weakly typed language and a lot more forgiving about how we use different types of data. When we deal with data, we often don't need to specify what type of data it is; JavaScript will work it out for itself. Furthermore, when we are using different types of data at the same time, JavaScript will work out behind the scenes what it is we're trying to do.
Given how easygoing JavaScript is about data, why do we need to 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, 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 we want it to do. In these situations, we need to make it explicit to JavaScript what sort of data type we intended and how it should be used. To do that, we first need to know a little bit about data types.
A second reason is that data types enable us to use data effectively in our code. The things that can be done with data and the results we'll get depend on the type of data being used, even though we don't specify explicitly what type it is. So, while trying to multiply two numbers together makes sense, doing the same thing with text doesn't. Also, the result of adding two numbers is very different from adding text. With numbers we get the total of their sum, but with text we get one big piece of text consisting of the other pieces of text joined together.
Let's take a brief look at some of the more commonly used data types: numerical, text, and Boolean. We will see how to use them later in the chapter.
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: –253 to 253.
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 we 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 us so we generally don't need to worry about it. One exception is when we want an integer but we have a floating-point number, in which case we'll round the number to make it an integer. We'll discuss rounding numbers later in this chapter.
Another term for one or more characters of text is a string. We 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".
What if you want a string with a single quote mark in the middle, say a string like Peter O'Toole? If you enclose it in double quotes, you'll be fine, so "Peter O'Toole" is recognized by JavaScript. However, 'Peter O'Toole' will produce an error. This is because JavaScript thinks that your text string is Peter O (that is, it treats the middle single quote as marking the end of the string) and falls over wondering what the Toole' is.
Another way around this is to tell JavaScript that the middle ' is part of the text and is not indicating the end of the string. You do this by using the \ character, which has special meaning in JavaScript and is referred to as an escape character. The \ tells the browser that the next character is not the end of the string, but part of the text. So, 'Peter O\'Toole' will work as planned.
What if you want to use a double quote inside a string enclosed in double quotes? Well, everything we just said about the single quote still applies. So 'Hello "Paul"' works, but "Hello "Paul"" won't. However, "Hello \"Paul\"" will work.
JavaScript has a lot of other special characters, which can't be typed in but can be represented using the escape character in conjunction with other characters to create escape sequences. The principle behind this is similar to that used in HTML. For example, more than one space in a row is ignored in HTML, so we represent a space by . Similarly, in JavaScript there are instances where we can't use a character directly but must use an escape sequence. The following table details some of the more useful escape sequences:
Escape Sequences |
Character Represented |
---|---|
\b |
Backspace |
\f |
Form feed |
\n |
New line |
\r |
Carriage return |
\t |
Tab |
\' |
Single quote |
\" |
Double quote |
\\ |
Backslash |
\xNN |
NN is a hexadecimal number that identifies a character in the Latin-1 character set. |
The least obvious of these is the last, which represents individual characters by their character number in the Latin-1 character set rather than by their normal appearance. Appendix D contains the full Latin-1 character set. Let's pick an example: Say we wanted to include the copyright symbol © in our string. What would our string need to look like? The answer is "\xA9 Paul Wilton". If you look at the table in Appendix D, you'll see that the character number of the copyright symbol is the hexadecimal number A9, so this is the number we need.
There is a similar way of referring to characters using their Unicode escape sequence. However, this is only valid in JavaScript version 1.3 and later (that is, IE 4.0+ and NN 4.06+). These are written \uNNNN, where NNNN refers to the Unicode number for that particular character. For example, to refer to the copyright symbol using this method, you would use \u00A9.
The use of yes or no, positive or negative, and true or false is commonplace in the "real" world. The idea of true and false is also fundamental to digital computers; they don't understand maybes, only true and false. In fact, the concept of "yes or no" is so useful it has its own data type in JavaScript: the Boolean data type. The Boolean type has two possible values: true for yes and false for no.
The purpose of Boolean data in JavaScript is just the same as in the world outside programming: it allows us to answer questions and make decisions based on the answer. For example, if I ask you, "Is this book about JavaScript?" you would hopefully answer, "Yes it is," or you might also say, "That's true." Similarly we might say, "If it's false that the subject of the book is JavaScript, then put it down." Here we have a Boolean logic statement (named after its inventor George Boole), which asks a question and then does something based on whether the answer is true or false. In JavaScript, we can use the same sort of Boolean logic to give our programs decision-making abilities. We'll be taking a more detailed look at Boolean logic in the next chapter.