Understanding Types


Programming languages can only deal with a limited number of types of things. By way of illustration, suppose you’re at the beach building a sand castle. There are three buckets you can use. One bucket can hold only sand, another bucket can only hold water, and the final bucket can only hold pebbles. So the types you have available for building your castle in the sand are sand, water, and pebbles—and each type must go in the appropriate bucket for its type. In other words, you’re not allowed to put water in the sand bucket and so on. In the context of a programming language such as JavaScript, the values that programs manipulate are of various kinds called types. JavaScript has three basic types (or kinds of value):

Boolean: A Boolean type stores logical information. It has a value of either true or false. (Note that the constant values true and false are case sensitive, meaning that they have to be written exactly as either true or false in all lower-case letters.)

Number: A Number type is an integer or floating-point value. For example, as I’m sure you know, 42 is a number, as is 3.1415.

String: A String type is some text, for example, "I think, therefore I am". Strings start and end with either matching single quotes or matching double quotes. If you don’t have the quotation marks, you don’t have a string. You should also know that you can have a string with no characters (the empty string). Here’s how it looks: "". Strings are so important in programming that I’ve written a whole chapter (Chapter 9, “ Manipulating Strings ”) just about them.

start sidebar
Advanced—Fancy Ways to Write Numbers

You can write numbers in standard decimal notation or in exponential, hexadecimal, or octal notation. For example, the four numbers shown in Table 2-1 all are 255, each notated as indicated. If this makes sense to you, fine; if not, please don’t worry about it—we’ll only use simple integers in this book, anyway!

end sidebar

Table 2-1: Fancy Numerical Notation

Notation

Representation

Decimal

255

Exponential

2.55e+2

Hexadecimal

0xFF

Octal

0377




Learn How to Program Using Any Web Browser
Learn How to Program Using Any Web Browser
ISBN: 1590591135
EAN: 2147483647
Year: 2006
Pages: 115
Authors: Harold Davis

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net