February 2, 2009
Data Types - String
There are a number of different types of data that
JavaScript can work with. The different types of data are
Numbers, Strings, Boolean, null, and undefined. Each is used
in a slightly different way for varying functionality.
Understanding each data type is of paramount importance when
you begin to write more complex scripts that require the use
of more than one type of data.
A String is basically a textual string of letters or an
alphanumeric string of letters and numbers. Also included
are special characters such as the copyright (©) character
and punctuation marks. All of the characters used within the
string must conform to the ISO-Latin-1 character set. Of all
of the data types, the string data type is used most often.
The String data type is defined by placing the string within
quotes, such as "This is a string of text". You may place
single quotes within double quotes. If you require double
quotes within the string, use single quotes to encapsulate
the string, such as 'This is the "string" of text'. If at
any time you require double quotes to be present within
double quotes, you will have to use what are called escape
characters. The escape character for a double quote is \" -
fairly easy to remember. A full list of escape characters
used within JavaScript is given below.
- \b - Represents a backspace - the pressing of the
"backspace" key.
- \f - Represents a form feed, which is a function
of your printer.
- \n - Represents a new line. The characters
following will be placed on a new line.
- \r - Represents a carriage return - the pressing
of the "enter" key.
- \t - Represents a tab space - the pressing of the
"tab" key.
- \' - Represents a single quote.
- \" - Represents a double quote.
- \\ - Represents a backslash.
- \XXX - Represents a character represented by
three digits in the octal numbering system to a maximum of
377 octal.
- \XX - Represents a character represented by two
digits in the hexadecimal numbering system to a maximum of
FF hexadecimal.
- \uXXXX - Represents a Unicode character
represented by four hexadecimal digits.
Using the above escape characters, examine the following.
var textString = new String("The word \"this\" is in double quotes");
The above example utilizes the double quote escape character
to illustrate how it is use within a string. The above
example, when written to your browser screen, will look as
follows.
The word "this" is in double quotes
You should have an idea of how the escape characters are
used now. That's it for the String data type.
Data Types - Other Types
There are a number of different types of data that
JavaScript can work with. The different types of data are
Numbers, Strings, Boolean, null, and undefined. Each is used
in a slightly different ways for varying functionality.
Understanding each data type is of paramount importance when
you begin to write more complex scripts that require the use
of more than one type of data.
Within the computing worlds, you will encounter many
instances where a data type of Number or String just will
not do. JavaScript has provided for this with three types of
data that fill in the rest of the needs of today's
programmer. These data types are Boolean, null, and
undefined. Each is looked at in depth in the following
sections.
Boolean
Simply put, a Boolean data type is a piece of data that is
either "true" or "false". It has only these two values for
you to work with, but with it you can do some pretty
interesting things. It is something like a bridge between
the returned values of some objects and the output or action
you wish to perform via your script. It is sometimes easier
to think of the Boolean true as "yes" and the Boolean false
as "no". Some JavaScript objects will also see the Boolean
true (yes) as the number 1 and the Boolean false (no) as the
number 0 (zero).
Null
The null data type is the data type returned from a
JavaScript operation when the operation has no value. null
and 0 (zero) are not considered to be the same, which is a
common error because other languages treat null and zero as
the same value. Keep it in mind: null and zero are two
completely different values in JavaScript.
Undefined
The undefined data type is not in any way equal to the null
data type. It represents a situation where the script knows
what type of data should be present, although there is no
data present to be returned. When working with numbers, the
undefined data type is equal to NaN (which is not equal to
anything, including itself). When working with strings, the
undefined data type is equal to undefined (and nothing
else). When working with Boolean values, the undefined data
type is equal to the Boolean false.
Data Types - Numbers
JavaScript Introduction
Variables
The JavaScript Chronicles
JavaScript Introduction
Part 2: Data Types
Part 3: Arrays
Part 4: Operators
Part 5: Conditional Statements
Part 6: JavaScript Functions
Part 7: Pattern Matching - The RegExp Object
Part 8: Introduction to Server Side JavaScript
Part 9: Server Side JavaScript Mail Sending
Part 10: Server Side JavaScript and File Manipulation
Part 11: Working with Forms in JavaScript
Part 12: Getting to Know Dynamic HTML
|