# Data Types

There are two type classifiations in JavaScript, primitive types and composite types. There are also two special keywords, null and undefined.

Primitive types include number, string and boolean. Their names are 'number', 'string' and 'boolean' respectively.

In JavaScript, all numbers are representing the double precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic. The minimum number could be obtained from Number.MIN_VALUE and the maximum number could be obtained from Number.MAX_VALUE. According to ECMA Section 8.5 - Numbers, the largest exact integral value should be 253, but note that the bitwise operators will result in a signed 32-bit integer, such as ~, >>, and so on.

Like other languages, JavaScript uses numeric literal notation to specify numbers. By default, numeric literals are treated as decimal numbers. They can also be interpreted as octal numbers if they are preceded by a "0", or as hexadecimal numbers if they are preceded by a "0x". For example:
10 (decimal)
0677 (octal)
To represent a floating point number, you can use scientific notation. For example:
3.14
5.231E13 (5.231 * 1013)
1.31E-32 (1.31 * 10-32)
There are several special values. For examples, +Infinity (or simply Infinity) is the positive infinite number value and -Infinity is the negative infinite number value. They can also be got from Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY respectively. Besides, NaN (or Number.NaN) represents a "Not-a-Number" value. For example, 1 / 'two' will produce NaN . Note that NaN equals nothing, even if itself. That's to say that NaN is not equal to NaN. Use isNaN() if you want to know whether a number is NaN. For example:
js> NaN == NaN;
false
js> isNaN(NaN);
true
js> isNaN(1 / 'two');
true
js>

Strings are a primitive type in JavaScript. You can quote a series of characters by single or double quotations to get a string. Such as...
var str1 = 'text1';
var str2 = "text2";
JavaScript has no type for a character. If you want to represent a character, just quote a single character, but it's still a string type. For example:
js> var ch1 = 'A';
js> var ch2 = "B";
js> typeof ch1;
string
js> typeof ch2;
string
js>

The typeof operator is used to get the type name of a value. The return value of typeof is a string. For example, typeof will return 'number' if the operand is number. In the above example, the type of values stored in ch1 and ch2 are both 'string'. While writing string literals, you could use single or double quotations. The convention is to use single quotations. Double quotations are usually used as HTML attributes to prevent hassles from escaping characters. For example:
var html = '<input type="text" value="defalut">';
The boolean type has only two values: true and false. Using a boolean value as a operand of typeof will get a 'boolean'.

The composite types are so-said objects. Basically, they are all instances of Object. The typeof operator returns 'object' if the operand is a composite type. For example:
js> var o1 = new Object();
js> var o2 = {};
js> var o3 = [1, 2, 3];
js> typeof o1;
object
js> typeof o2;
object
js> typeof o3;
object
js>

The keyword null is very special in JavaScript. The null means nothing. A variable can refer to null if you don't want it refer to anything. You can also test whether a variable refers to null.
js> var x = null;
js> x == null;
true
js>

If you want to know the type of an object, you can use the instanceof operator. For example:
js> var x1 = {};
js> var x2 = [];
js> x1 instanceof Object;
true
js> x2 instanceof Object;
true
js> x2 instanceof Array;
true
js>

It's weird that typeof null returns 'object' and just can be learned by rote. If you test whether null is the instance of Object, you will get false.
js> typeof null;
object
js> null instanceof Object;
false
js>

Take care here. The following x actually has a null value. It's not undefined.
var x = null;
The following x is undefined.
var x;
The keyword undefined is a special keyword in JavaScript. You get undefined if you try to retrieve a value from variables or properties without being specified anything. The expression typeof undefined return 'undefined'. Take care, undefined shows nothing in Rhino Shell.

Don't mistake undefined with the 'not defined' message while interpreting. For example, the following code will throw a "not defined" interpreter error:
js> var x = y;
js: "<stdin>", line 2: uncaught JavaScript runtime exception: ReferenceError: "y" is not defined.
at <stdin>:2

js>

In Chrome Developer Tools, the error looks like the following. The following code can be executed correctly but values of x and y are both undefined:
var x;
var y = x;

undefined equals undefined:
js> undefined == undefined;
true
js>