JavaScript Data Basics

Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site

JavaScript: The Big Picture

When you understand the connectivity of the following JS concepts, you have mastered most of the mystery of the JavaScript language:

Its a vicious circle, but not exactly a circular relationship. We will see that literals are the drivers for these relationships. It is true that class constructors of a native class type can be responsible for objects, thus data types and of course data. And we cover native objects elsewhere. But most of us use literals to populate variables of the various data types.

A Literal Determines Data Type

As we populate a variable with a literal, JS looks at that literal token and figures out the type of data it represents. Because JavaScript is weakly typed, JS determines the data type by analyzing the literal syntax, range of values and other factors such as keywords. This is discussed in detail at this page "ECMA-262 Core: JavaScript Literals". But the following table gives a summary:

Literals Determine the Data Type
Data Type Determining Factors Literal Example
Literal Syntax Range of Values Other
string quotes n/a n/a var s = "a string";
boolean n/a true, false n/a var b = true;
number n/a integer and floats
have a set range of numbers
floats: have decimal point var n = 241;// integer var n = 1.65409;// float
Array square brackets n/a comma separator var primes = [2,3,5,7];
Function parentheses
curly braces
n/a function keyword var area = function(x,y)
{ return x*y; };
Object curly braces
n/a comma separator var myobject = {
fname: "Fritz",
lname: "Fry",
add1: "12345 S. Street",
city: "Town",
state: "CA",
zip: null

We have additional pages that will enhance the understanding of the above topic:

The Data Type Determines the Object

We just saw how the literal token determines the data type. On the next topic, JS has a subtle almost deceptive behavior. That is, variables defined with one of the above six data types, become and behave like an object. It is obvious to the JS practitioners that when objects are created with class type constructors, they should take on the behavior of an object of that class type. But when variables are created via a literal token, we also magically acquire object behavior. By object behavior, we mean that the variable has access to the methods and properties of the class associated with the data type of the variable.

In summary, the literal determines the data type and as an encore, we also get object behavior that corresponds to the class type representative of the data type.

The Data Type Describes the Data and More

Besides helping to determine the applicable object of a given variable, the data type has many other implications:

Data is Created by Literals

So, the literal determines the data type, which inturn determines the object. The data type describes the interpretation, the storage, the retrieval, the operations and error checking of the data. Where does the data come from? Besides class constructors, expressions and built-in values, most data in a JS program will come from literals.

Now you understand the JS connectivity between the data, the data type, the defining literal and the associated object.

Exceptions to the Rule

• The Regular Expression Object
We could have included the RegExp Object in our discussion here. The RegExp Object has it own unique literal syntax. A variable assigned to a regular expression literal will put on the characteristics of the RegExp Object. However, regular expressions do not have a corresponding data type, so they are excluded from this discussion.

• The Date Object
JS does not have a date data type. There are no literal date formats. You must use the constructor of the Date class type to create your dates.

• The Error Object
The same said for Date Objects applies to Error Objects.

• The Null Data Type
ECMAScript designates the Null data type as one of five primitive data types. We exclude this type from our discussion since the Null type does not have a corresponding class type.

• The Undefined Data Type
The same said for Null Data Type applies to Undefined Data Type.


Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson