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

The data type in JavaScript is determined by the literal token. This is part of weak typing. The actual content and syntax of the literal token determines the data type of the related variable. Generally, JS primitive data is typed based on content. That is, a literal token can be typed based on a set range of values that represents a JS data type. Composite data types are typed based on the syntax of the literal token.

The acceptable literal tokens for each of the five primitive data types are summarized in the following table.

Data Type | Literal Syntax | Comments | Range of Values & Examples |
---|---|---|---|

Footnotes: | The range of values for a given type is in bold. | ||

string | Quotes Single or Double |
A string is enclosed in matching pairs of either single or double quotes. String Type Character Set for Strings Nesting Quotes Apostrophes and Single Quotes |
String literals may contain any character in: The Unicode character set var c1 = "pink"; var c2 = 'red' ; var c3 = "you\'re"; |

number | Decimal values including exponent, hexadecimal and octals | Integers: whole numbers Number Type The IEEE 754 Standard Negative Integers Octal Representation Hexadecimal Representation |
integer minimum number: -9007199254740992 or -2 ^{53}
integer maximum number: +9007199254740992 or +2 ^{53}
var num1 = 23; |

Floats: have decimal point and fractional values MAX_VALUE Constant MIN_VALUE Constant Format for Floats |
float largest numbers: 1.7976931348623157 X 10 ^{308}
float smallest numbers: 5 X 10 ^{-324}
var num1 = 23.667; |
||

boolean | true, false | A boolean value is of the type Boolean and is one of two unique values, true and false. Boolean Type |
There are exactly two legal literal values: true: meaning, true, on or 1 false: meaning; false, off or 0 var stop = true; |

null | null | The type Null has exactly one value null. When a variable contains null, it does not contain a legal value for the implied type. Null Type |
The literal for the Null type is also the keyword null. var status = null; |

undefined | The ECMA-262 does not provide a keyword for the undefined type. However it is a property of the Global object. | The Undefined type has exactly one value, undefined. When a variable is declared but not initialized, it is given the value of undefined. Undefined Type How Null and Undefined Differ |
The undefined type does not have an associated literal. var status; |

Literals of composite type are distinguished from themselves and primitive types by the syntax of the literal token. The following table illustrates the literals for the three different composite types.

Data Type | Literal Syntax | Comments | Format and Examples |
---|---|---|---|

Footnotes: | The format for a given type is in bold. | ||

Array | brackets | Array literals are used with the enclosing bracket [ ] syntax. Array Type Array Constructor JS Arrays |
[element1, element2,....] var thearray = [1990, 1991, 1992, 2007]; |

Function | parenthesis | Function literals are used with the enclosing parenthesis( ) syntax. Function Type Function Constructor Function Statements |
(arg1, arg2,....) var sqrt = function(num) { return num * num;} |

Object | braces | Object literals are used with the curly braces { } syntax. Object Type Object Constructor |
{prop1: val1, prop2: val2, propN: valN,....} var myobject = { fname: "Fritz", lname: "Fry", add1: "12345 S. Street", city: "Town", state: "CA", zip: null } |

The RegExp object has a literal syntax that can be coded directly into JavaScript literals. The text between a pair of forward slashes ( /text/ ) make up the regular expression literal. The grammar of regular expressions is very complex. We have documented the subject of regular expressions at our page entitled "Regular expression in JavaScript".

The string type covers many special inline character literals (also known as escaped characters) . Escaped characters can trigger additional handling of the string. The mechanism of entering escape characters is to encode a backslash symbol followed by the character that triggers the task.

We have documented the JavaScript escape sequence set on our page entitled "The Escape Sequences for JavaScript".

JavaScript allows you to work with integer values in decimal (base: 10), octal (base: 8) and hexadecimal (base:16) formats. The following table illustrates the three integer formats with rules and examples for each format.

Format | Syntax Rule and Comments | Examples |
---|---|---|

Footnotes: | All mathematical operations return decimal values even when the literal token contains octal or hex values. | |

Decimal base 10 |
The 1st digit can not be a zero. | icnt = 23; |

Octal base 8 |
The 1st digit must be a zero.
All other digits may be zero thru seven. Octal Limitation |
icnt = 027;
an octal 27 is equal to a decimal 23 |

Hexadecimal base 16 |
The 1st digit must be a zero.
The second digit must be the letter x. All other digits may be 0-9 and A-F. The alpha digits can be upper or lower case. Hexadecimal Defined |
icnt = 0x17;
a hex 17 is equal to a decimal 23 |

- • Character Data Type:
- The character data type (single char) does not exist in JavaScript as it does in Java. Instead, you can use the string literal with a length of one.
- • Character Set for Strings:
- The EMCA-262 standard allows Unicode characters within string literals. Unicode is a 16-bit encoding scheme that has a total of 65,536 characters. To represent a character as Unicode, you must escape the code with a "\u" followed by the two byte code expressed in hexadecimal. For example, the letter "B" would be represented as "\u0042". Prior version of JavaScript before the EMCA-262 standard only allowed ASCII or Latin-1 characters.
- • Nesting Single and Double Quotes:
- Double quotes may be nested within strings delimited by single quotes. Conversely, single quotes may be nested within strings delimited by double quotes.
- • Apostrophes and Single Quotes:
- You must use the backslash escape sequences for any apostrophe that may appear in a single quote string. Similarly, any double quoted string containing a double quotes as part of the string must have the inline double quotes escaped.
- • The MIN_VALUE Constant:
- The Number.MIN_VALUE property is a constant that represents the smallest possible positive number that JavaScript can manage. The constant belongs to the Number object and has a value of 5 X 10
^{-324}. - • The MAX_VALUE Constant:
- The Number.MAX_VALUE property is a constant that represents the largest possible positive number that JavaScript can manage. The constant belongs to the Number object and has a value of 1.7976931348623157 X 10
^{308}. - • Format for Floats:
- To define a floating point number in JavaScript, you specify the traditional format for a real number. That is, an integer portion followed by a decimal point and at least one digit to the right of the decimal point. For example, the whole number 6 should be define as 6.0. Floating point values can be defined using e-notation (exponential notation). Here a number is given followed by an "e" or "E" and this is followed by the number of times to multiply by 10.
`37.5467e4 = 37.5467 X 10 X 10 X 10 X 10 = 375467`

E-notation can also be used to represent very small numbers. The number 0.000000078 can be coded as 78e-9. This is the same as dividing 78 by 10 nine times. - • The IEEE 754 Standard:
- The ECMA-262 standard comply's with the IEEE 754 standard. This standard allows for 64-bit (8-bytes) double-precision values with up to 17 decimal places. Values are truncated after 17 decimal points which can lead to mathematical errors for extreme precision math.
- • Negative Integers:
- JavaScript numbers can be preceded by a minus sign (-) to make a number negative. However, this technique is actual the negation operator (unary minus operator) at work.
- • Octal Limitation:
- Although most current version of JavaScript support octal literal representations, ECMA-262 does not support octal. Therefore, octal literals should be avoided. Also, avoid coding decimal integers with leading zeros.
- • Undefined Subtleties:
- Typically you view the undefined type as a variable that is declared but not yet initialized. What about a variable that has never been declared? Performing a typeof operation on such a variable would also give an undefined type. But trying to perform another operations on the same variable (undeclared and uninitialized) would result in an error.
- • How Null and Undefined Differ:
- The Undefined type means the value has not been set. The Null type means the value has been set to empty.
- • JS Classes and Literals:
- All JS Native classes have corresponding literal forms except for two classes. These two classes are the Date class and the Error class. Further, all JS Native classes have corresponding data types except for the same two classes. What gives?? The page "Data Basics" explains how JS uses the literal token to determine the data type. This is how weak typing works. Conclusion; if the class does not have a literal form, then it can not have a data type in a language that is dynamically typed.