There are five simple data types in ECMAScript: Undefined, Null, Boolean, Number, and String. There is also one complex data type: Object, which is an unordered list of name-value pairs.
Because there is no way to define your own data types in ECMAScript, all values can be represented as one of these six.
The typeof Operator
ECMAScript is loosely typed, so there needs to be a way to determine the data type of a given variable. The typeof operator provides that information.
- "undefined" if the value is undefined
- "boolean" if the value is a boolean
- "string" if the value is a string
- "number" if the value is a number
- "object" if the value is an object (other than a function) or null
- "function" if the value is a function
The Undefined Type
The Undefined type has only one value, which is undefined. When a variable is declared using var but not initialized, it is assigned the value of undefined.So the following two variables are identical to the other:
The Null Type
The Undefined type has only one value, which is null.Logically,a null value is an empty object pointer,so the following code will alert "object".
The undefined is a derivative of null,but they have very different uses. When defining a variable to hold an object later, it is recommended to initialize the variable to null as opposed to anything else.
The Boolean Type
The Boolean type has only two values: true and false.Note that they are case-sensitive.
The following values will be converted to false: false, ""(empty string), 0, NaN, undefined, null, false.
The Number Type
The Number in ECMAScript uses IEEE-754 format to represent both integers and floating-point values.There are three literal formats for integers.
To define a floating-point value, you must include a decimal point and at least one number after the decimal point.Because storing floating-point values will use twice as much memory as storing integer values, ECMAScript always looks for ways to convert values into integers.
NaN, short for Not a Number, which is used to indicate when an operation intended to return a number has failed (as opposed to throwing an error).In ECMAScript, dividing a number by 0 returns NaN.
The value NaN has a couple of unique properties. First, any operation involving NaN always returns NaN (for instance, NaN /10), which can be problematic in the case of multistep computations. Second, NaN is not equal to any value, including NaN.
The String Type
The String data type represents a sequence of characters. Strings can be wrapped by either double quotes (") or single quotes ('), so both of the following are legal:
The String data type includes several character literals to represent nonprintable or otherwise useful characters, as listed in the following table:
|\xnn||A character represented by hexadecimal code nn (where n is a hexadecimal digit 0-f). |
Example: \x41 is equivalent to “A”.
|\unnnn||A Unicode character represented by the hexadecimal code nnnn (where n is a hexadecimal digit 0-f). Example: \u03a3 is equivalent to the Greek character Σ|
The Nature of Strings
Strings are immutable in ECMAScript, meaning that once they are created, their values cannot change. To change the string held by a variable, the original string must be destroyed and the variable filled with another string containing a new value, like this:
Converting to a String
There are two ways to convert a value into a string.First, use the toString() method or String() casting function; second, use variable + ""(an empty string).
The Object Type
Objects in ECMAScript start out as nonspecifi c groups of data and functionality.The Object type in ECMAScript is the base from which all other objects are derived.
var obj = new Object();
Each Object instance has the following properties and methods: