Team LiB
Previous Section Next Section

Data Types

Data types are the way that data can be represented. For example, if you saw the characters 123, you would most likely assume that they were a number, and correctly so. What you may not think of is that the same three characters can also be a string "123". Both ways of thinking about the three characters are correct, but sometimes one way has advantages over the other. For instance, you would want to worry about using the string representation in an arithmetic operation. On the other hand, the string representation of the characters 123 is easier to print to the screen than the number representation. This is an example of choosing the correct primitive data type for a specific job.

Primitive data types are not the only group in a language's data type collection. Many languages, including JavaScript, also have a collection of abstract data types. In the case of JavaScript, the abstract data types are usually objects. Objects are most easily thought of as a collection of primitive data types all kept in one place. You will see in Chapter 5 that an object has many properties that are a part of its definition. These properties are usually instances of primitive data types. In addition to a collection of primitive data, objects can also have functions that are part of their definition.

Primitive Types

Because JavaScript is an interpreted language, it has the added advantage of being loosely typed. What that means exactly is that a variable does not need to be implicitly declared as one of the built-in JavaScript types. Instead, a variable can be declared using one type of literal (number, string, or boolean) and used as one or the other. There are three primitive types in the JavaScript programming language: number, string, and boolean.

The number primitive type is used to perform arithmetic operations such as addition, subtraction, multiplication, and division. Any whole number or floating-point literal that does not appear between quotation marks is considered a number.

The string primitive type is used to handle text. A string represents any sequence of zero or more characters that are to be considered strictly text—that is, no mathematical operations can be performed on them.

The boolean primitive type is used with logical operations and can have one of two values: true or false.

Table 1.2 lists each primitive data type that is part of the JavaScript language, as well as several examples of each.

Table 1.2: Primitive Data Types

Data Type

Examples


Number

57, 3.14, 1001

String

"Hello", "a string", "2001"

Boolean

true, false

These three data types comprise the majority of data you will encounter while using JavaScript.

Abstract Data Types

JavaScript's abstract data types are extremely useful. Abstract data types include objects, arrays, and functions.

An object represents a collection of data and functions that work together to perform a related task. One of JavaScript's most powerful features is its object-based approach. All abstract data types, including strings, arrays, and functions, are objects. This approach makes objects very efficient and easy to use. Although strings and arrays are considered objects in JavaScript, their behavior is slightly more specialized than the other general objects, which is why they are covered in their own chapter, Chapter 3. Objects are covered in depth in Chapter 5, "Objectoriented JavaScript."

An array facilitates the storing of like data in a logical way. For more information on arrays, see Chapter 3, "JavaScript Arrays and Strings."

A function is a piece of executable code that is written once but is called repeatedly from different parts of your program. Functions are covered in Chapter 4, "JavaScript Functions."

Special Data Types

There are three built-in special data types in the JavaScript programming language. They are null, undefined, and NaN and are explained in Table 1.3.

Table 1.3: Special Data Types

Data_type

Description

null

This value is given to a variable to indicate that no value exists.

undefined

This value is given to a variable to indicate that it has not been previously defined.

NaN

Not-a-Number is used to signify that a number variable is in fact not a legal number.

Following is an example demonstrating how each of the special data types are produced:

<html>

  <head>
    <title>
      JavaScript Profesional Projects - Special Data Types
    </title>
    <script language="JavaScript">

    <!--
      var myNumber;
      var myDate = new Date();
      delete( myDate );
      myDate = null;
    // -->
    </script>
  </head>

  <body onLoad="javascript: sayHello()">

    <center>
      <font size=6>JavaScript Professional Projects</font><br>
      <font size=4>Chapter 1: Special Data Types</font>
    </center>

    <br><br>

    <p>
    The variable 'myNumber' currently has the value: <b>
    <script language="JavaScript">
    <!--
      document.write( myNumber ); 
    // -->
    </script>
    </b>because it has not been defined yet.
    <br><br>
    The variable 'myNumber' is now being assigned the value 'text'.<br>
    The variable 'myNumber' currently <b>
    <script language="JavaScript">
    <!--
      document.write( isNaN( myNumber ) ? "is not" : "is" );
    // -->
    </script>
    </b>a number!
    <br><br>
    The object 'myDate' currently has the value: <b>
    <script language="JavaScript">
    <!--
      document.write( myDate );
    // -->
    </script>
    </b>because it was deleted before being used!
   </p>

  </body>

</html>

Team LiB
Previous Section Next Section