Free JavaScript Editor
JavaScript Debugger
4.1. Data TypesAs with its ancestor, the C programming language of Kernighan and Ritchie, JavaScript supports a number of data types. Although the number isn't nearly as large as C, representatives of the basic data types are all present, and methods of describing your own data types exist. In fact, with only a little delving into the "dark arts," it is quite possible that many problems can be solved on the client side using JavaScript. 4.1.1. NumericIn JavaScript, all numbers are 64-bit double-precision floating-point numbers, whether they are floating point or integer. This means that 18,437,736,874, 454,810,624 values divided evenly between positive and negative can be represented in JavaScript. In addition, there are three "special" values, increasing the total to 18,437,736,874,454,810,627. And you thought that you were being robbed. The first of the three "special" values is NaN, which means Not a Number or "oops," as I like to think of it. From my point of view, it means that I made some kind of boneheaded mistake and am doomed to suffer for it. The second and third values are positive and negative infinity, which are, well, infinite. 4.1.2. StringJavaScript strings are UTF-16 strings or 16-bit Unicode Transformation Formats: character encoding. What it comes down to is that each character in a string is represented in 2 bytes, which means that the potential for display of non-English characters exists. This might not seem like a big deal, but it very well could be when the boss walks into your office and asks about internationalization. Ooh, scary. Seriously, though, quite a number of things can be done in JavaScript along the lines of string manipulation. For example, it is quite easy to make an entire line either upper case or lower case, a really nice feature when testing for a particular string value. In addition, other functions allow for the searching, extracting, and replacing of substrings. Table 4-1 outlines these features.
In my opinion, one of the coolest ways to manipulate strings has got to be regular expressions, although, come to think of it, it is also probably one of the most obscure ways to manipulate strings as well. If you're unfamiliar with regular expressions, they are an object that stores a pattern for use in the searching of strings. This pattern is then applied to a string using either a regular expression method or a string method. The theory behind regular expressions is relatively easy to grasp, but the actual practice is not. The reason for this comes down to the pattern; it needs to be specific enough to find only what you are actually looking for, yet it also needs to be general enough to be able to find sequences that aren't always easy to find. Maybe you'll be able to understand how this works a little better after looking at Table 4-2, which describes the special characters that go into constructing a pattern.
Alright, now for a quickie example. Let's say, for instance, that we want to replace all instances of either the word red or the word blue in a string with the word purple. Although this could be done programmatically, as shown in Listing 4-1, it isn't the easiest thing in the world. However, with a regular expression, also shown in Listing 4-1, it really isn't too bad. Listing 4-1. Programmatic and Regular Expression Approaches to String Substitution
I would like to point out that, at the time of this writing, Microsoft Internet Explorer appears to have a bug with regular expressions. It occurs when performing regular expressions in a loop. Occasionally, even though a pattern match exists, it isn't recognized. Fortunately, there is a workaround. Within the body of the loop, use the compile method to "reset" the pattern. When this is done, pattern matches are always recognized. Yes, it is something of a kludge, but regular expressions are too useful to ignore, and we should also be kind to those less fortunate than ourselves by accommodating their broken web browsers. 4.1.3. BooleanJavaScript Boolean data types are the standard true/false data types that we've all been exposed to umpteen times, end of story. 4.1.4. MiscellaneousThese are the two data types that don't cleanly fit into any category: null and undefined. The null data type represents nothing, and the undefined data type represents something that is not defined. 4.1.5. ArraysAlthough it's not an object type, I've chosen to include arrays here because they are a useful mechanism for grouping related information. A relatively simple data structure, arrays permit the access of information based upon an integer index. In JavaScript arrays, this index begins at zero and increases by one for each element of the array. An item of interest about arrays in JavaScript is that it isn't necessary for the individual elements of an array to all be of the same type, although it might be a good idea to ignore this capability because it presents a world of opportunities to really screw up. However, some really nice goodies built into JavaScript more than make up for the potential issues that might arise from weak typing. First things first. Let's take a look at the three ways to define a JavaScript array: defining an empty array, defining an array with a set number of elements, and defining an array with a set number of elements with values. Each of these three ways uses the Array() constructor, as shown in the following snippets:
var one = new Array();
var two = new Array(3);
var three = new Array('red', 'green', 'blue');
Earlier I stated that there are some really nice goodies built into JavaScript arrays, but they're rather numerous, so I've chosen to list them in Table 4-3.
4.1.6. ObjectIn JavaScript, the Object type is an unordered collection of name and value pairs. Although this doesn't sound like much, it is a type of data structure that is commonly referred to as an associative array. I have a tendency to use an associative array. |
Free JavaScript Editor
JavaScript Debugger