JavaScript Data Types

Every variable in JavaScript has a data type which dictates the values that can be stored in it.  However, JavaScript is a weakly typed programming language, which means that a variable is not constrained to a single data type for its entire lifetime.  In many ways, this makes a programmer’s life more difficult because a variable can potentially be holding any type of data at any given time.  This requires the programmer to thoroughly understand how the different data types behave, as well as how they interact with each other.  This is the first in a series of posts which explores the intricacies of JavaScript data types.

JavaScript data types can generally be divided into two categories ― primitives and objects.  Primitives represent the most basic types of data supported by a language.  In JavaScript, the primitive data types are Undefined, Null, Boolean, Number, and String.  Objects, on the other hand, are composite data types.  Each object is a collection of primitives and other objects.  The following sections describe the primitive data types in detail.  The object type will be revisited in a future post.

Undefined Data Type

The Undefined data type represents an absent or unknown value.  This data type consists of a single value, undefined.  Variables which have not yet been assigned a value default to the undefined value.  Variables can also intentionally be assigned the undefined value, although this is relatively uncommon*.  The following example shows two variables which are both undefined.  The first variable, “foo”, is undefined because no value is assigned to it.  The second variable, “bar”, is intentionally set to undefined.

var foo;
var bar = undefined;

*The undefined value is not directly assigned.  There is actually a global variable named “undefined” whose value is the undefined value.

Null Data Type

Much like the Undefined type, the Null data type also represents the absence of a value.  Unlike Undefined, Null represents an intentional lack of a value.  The Null data type is represented by a single literal, null.  The null value is often used to initialize or clear object variables.  The following example shows how the null value is assigned to a variable.

var foo = null;

Boolean Data Type

The Boolean type consists of two literals, true and false, which correspond to the truth values of Boolean logic.  Boolean variables are typically used to represent the results of a comparison (less than, greater than, etc.).  They are also useful for representing the presence, or lack thereof, of a value.  An example of this is a checkbox which is either checked or not checked.  In the following example, the “isSet” variable is set to false.  The example also stores the result of a comparison in “isGreater”.  Since two is greater than one, “isGreater” is true.

var isSet = false;
var isGreater = (2 > 1);

Number Data Type

All numeric data is represented by the Number data type.  This type includes both negative and positive numbers.  Negative numbers are always preceded by a minus sign.  Positive integers can be preceded by a plus sign, but it is not required.  Numbers can be formatted in a variety of ways.  The following list describes each of these ways.  All numbers are assumed to be base ten unless otherwise noted.

  • Integers ― Integers are positive and negative numbers that do not have a fractional part.
  • Real numbers ― Real numbers can have both an integer and a fractional part.
  • Scientific notation ― Scientific notation is useful for representing extremely large and extremely small values.  Values represented in scientific notation are formatted as a coefficient, followed by the letter “e”, followed by an exponent.  The coefficient can be an integer or real number, and the letter “e” can be either lowercase or uppercase.  The exponent, however, must be an integer.
  • Hexadecimals ― Hexadecimal (or hex) values are base sixteen integers.  Hexadecimal uses the ten decimal digits 0-9 and the letters A-F to represent numbers.  Hex is often used as shorthand for representing binary values.  Hexadecimal values must begin with the characters “0x”.  Hex values are not case sensitive.  Therefore, the values 0XDEADBEEF and 0xdeadbeef are equivalent.
  • Octals ― Octal values are base eight integers.  Octal numbers can only include the decimal digits 0-7.  JavaScript octals are specified by adding a leading zero to a number.  However, because people tend to subconsciously ignore leading zeros, octal notation can easily be confused with base ten notation.  To prevent this problem, octal values are prohibited in strict mode.
The following example shows how each number format is written.
var integer = 100;
var real = 3.14; // pi
var scientific = 3.14e2; // 314, or 3.14 * 10^2
var octal = 0144;  // base ten value is 100
var hexadecimal = 0x64; // base ten value is 100

Although programmers can specify numeric data in several formats, internally JavaScript stores all numbers as floating point numbers.  A floating point number is a binary representation of a real number.  There are several ways to implement floating point numbers, however JavaScript uses the IEEE-754 standard.  One interesting note is that IEEE-754 defines two zero values, +0 and -0.  The two values are generally treated as the same number, however it is useful to be aware of the distinction.  The IEEE standard defines several other special numbers, which are explained below.

Not-a-Number

The Number type includes a special “Not-a-Number” (NaN) value which represents unrepresentable numbers.  NaN can be used in mathematical computations, but any such computation will result in NaN.  The following example includes three statements which evaluate to NaN.  The first statement divides zero by zero.  The result is undefined as a real number and is assigned the value NaN.  The second statement computes the square root of a negative number.  The result is an imaginary number, and is therefore treated as NaN.  The third statement assigns the NaN value directly to a variable.

var foo = 0/0;  // foo equals zero divided by zero
var bar = Math.sqrt(-1); // bar equals the square root of -1
var baz = NaN;

Infinity

The Number type can also represent the mathematical concept of infinity.  When used in code, infinity is represented by the value Infinity.  Negative infinity can also be expressed as -Infinity.  Arithmetic involving infinity is governed by the rules in the following list.  Note that JavaScript’s rules regarding infinity are not always the same as those in mathematics.

  • Any finite number added to, or subtracted from, Infinity is Infinity.
  • Any finite number added to, or subtracted from, -Infinity is -Infinity.
  • Adding Infinity and -Infinity yields NaN.
  • Any positive value (including Infinity) multiplied by Infinity is Infinity.
  • Any positive value (including Infinity) multiplied by -Infinity is -Infinity.
  • Any negative value (including -Infinity) multiplied by -Infinity is Infinity.
  • Any negative value (including -Infinity) multiplied by Infinity is -Infinity.
  • Zero multiplied by Infinity or -Infinity is NaN.
  • Any finite value divided by Infinity or -Infinity is zero.
  • Infinity divided by any finite positive value is Infinity.
  • Infinity divided by any finite negative value is -Infinity.
  • -Infinity divided by any finite negative value is Infinity.
  • -Infinity divided by any finite positive value is -Infinity.
  • Infinity divided by Infinity or -Infinity is NaN.
  • -Infinity divided by Infinity or -Infinity is NaN.
  • Any positive value (including Infinity) divided by zero is Infinity.
  • Any negative value (including -Infinity) divided by zero is -Infinity.

String Data Type

Textual data is represented by the String data type.  A JavaScript string is an ordered sequence of zero or more characters.  String literals are created by enclosing a character sequence within double or single quotes.  The choice of using single or double quotes is purely stylistic.  The only restriction is that the opening and closing quote must be of the same type.  In other words, strings beginning with a double quote must also end with a double quote.  Similarly, strings that start with a single quote must be terminated with a single quote.  It is also important to realize that the opening and closing quotes are not part of the string value.  The quotes are merely used to delimit the beginning and end of the string.  The following example assigns three string literals to variables.  The first variable, “foo”, stores a string enclosed in double quotes, while the string stored in “bar” uses single quotes.  The third string, stored in “baz”, is a special string containing no characters, known as the empty string.

var foo = "Hello World!";
var bar = 'Hello Again!';
var baz = "";

Escape Sequences

One shortcoming of string literals is that they cannot represent certain characters.  For example, a double quote cannot appear within a double quoted string literal because it would be interpreted as the string’s terminator.  String literals are also incapable of representing non-printing characters such as tabs and line breaks.  In order to represent problematic characters, JavaScript provides escape sequences.  An escape sequence is a combination of characters, beginning with a backslash, that is interpreted as a single character (referred to as the escaped character).  The character(s) following the backslash determine the escaped character.  The following list enumerates JavaScript’s escape sequences and the characters that they represent.

  • \b ― Backspace
  • \t ― Horizontal tab
  • \n ― Line feed (new line)
  • \v ― Vertical tab
  • \f ― Form feed
  • \r ― Carriage return
  • \” ― Double quote
  • \’ ― Single quote
  • \\ ― Backslash
  • \xXX ― Latin-1 encoded character specified by two hexadecimal digits.  The hex value must be between 00 and FF.  For example, \xA9 represents the copyright symbol.
  • \uXXXX ― Unicode character specified by four hexadecimal digits.  For example, the copyright symbol is specified by \u00A9.
  • \XXX ― Latin-1 encoded character specified by up to three octal digits.  The octal value must be between 0 and 377.  Like octal numbers, octal escape sequences are also prohibited in strict mode.

The following example creates a string literal which contains several common escape sequences.  Note the escaped double quote characters and the new line escape sequence.

var str = "Say \"Hello World\"\nAnd start a new line";

The previous string looks somewhat convoluted.  However, when the string is displayed, the escape sequences are replaced, resulting in the following output.

Say "Hello World"
And start a new line

It is also possible to escape characters that do not have any special meaning.  For example, the escape sequence \c is simply replaced by the letter “c”.  However, this should be avoided as there is no logical reason for doing so.

Things to Remember

  • JavaScript is a weakly typed language, meaning that a variable’s data type can change during execution.
  • The primitive data types are Undefined, Null, Boolean, Number, and String.
  • The Undefined type represents an absent or unknown of value.
  • The Null type represents an intentional lack of value.
  • Boolean values can hold two values, true and false.
  • Numeric data is stored in the IEEE-754 floating point format.
  • The String type is used to store textual data.

One thought on “JavaScript Data Types