Data Types



index
Disabled back button Next Section
printable version

Section 1: Definition

A value, the data assigned to a variable, may consist of any sort of data. However, JavaScript considers data to fall into several possible types.

Depending on the type of data, certain operations may or may not be able to be performed on the values.

Data types describe the information that a variable can store, and also describe how many bytes of memory are required to represent a type. Variables can be these types:

Numbers 3 or 7.987, Integer and floating-point numbers.
  • Integers
    • Integers can be positive, 0, or negative
    • Integers can be expressed in decimal (base 10), hexadecimal (base 16), and octal (base 8)
    • A decimal integer literal consists of a sequence of digits without a leading 0 (zero).
    • A leading 0x (or 0X) indicates a hexadecimal integer, which can include digits (0-9) and the letters a-f (or A-F).
    • The hex value A1 would be 161 in decimal notation.
    • Converter
    • Conversion Process
  • A floating-point number can contain either a decimal point, an "e" (uppercase or lowercase), which is used to represent "ten to the power of" in scientific notation, or both.
Booleans The possible Boolean values are true and false.
  • These are special values, and are not interchangeable with 1 and 0.
  • However, in a comparison, any expression that evaluates to 0 casts to false, and any statement that evaluates to a number other than 0 casts to true.
Strings Strings consist of a series of characters.
  • Strings are delineated by single or double quotation marks.
  • "Hello World!"

That said, JavaScript is a loosely typed language – you do not have to specify the data type of a variable when you declare it, and data types are converted automatically as needed during script execution.

Section 2: Numbers

Numbers are the easiest of the data types to understand. They represent numeric values.

integer

The simplest type of number is an integer. An integer is a base-10 whole number. In other words, it is a number with no fractional component. All of the following are valid integers.

JavaScript can handle integer values between 21024 and -21024.

Floating point

Another commonly used type of number is a floating-point number. A floating-point number is a number that is assumed to have a decimal point. Floating-point numbers have a fractional component, even if that fractional component is zero. They can be represented as a real number (integer – decimal point – fractional value) such as in 3.1714, or using exponential notation.

Section 3: Numeric Keyword Literals

JavaScript also has some numeric keyword literals you should know about. These are listed below.

Infinity Infinity has the value of infinity, which is to say a number larger than the largest number that JavaScript can represent.
  • Not really infinity, but from a computing perspective, it might as well be.
  • There is also the keyword literal -Infinity for the negative infinity.
  • As well as keywords, these can be represented as properties of the Number object as Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY.
  • Using the Number object properties comes from older JavaScript, while the keyword literals were first implemented with JavaScript 1.3.
  • You can test for infinity with the isFinite() function.
NaN NaN is the value returned when you try to treat something that is not a number as a number. For instance the results of 7 times "abc" is not a number. The old form of it is Number.NaN. You can test for not-a-number values with the isNaN() function.
Number.MAX_VALUE Number.MAX_VALUE represents the largest representable number for JavaScript.
Number.MIN_VALUE Number.MIN_VALUE represents the small representable non-zero number for JavaScript. In other words as close as JavaScript can get to zero with actually reaching zero.
Section 4: Boolean Values

There are two Boolean values, true and false. These are normally the result of a logical comparison in your code that returns a true/false or yes/no result, such as

a == b

This statement reads: does the value of variable a equal the value of variable b?


When testing for the result of a comparison, JavaScript will treat any non-zero value as true, and a zero value as false. JavaScript will also test to false when you test for the existence of something that does not exist.

You declare Boolean variables like this:

var finished = false;
var passing = true;

Section 5: Strings

A string is sequence of Unicode characters within a given character set. It is normally used to represent text. A string literal is defined by enclosing it in matching single or double quotes.

If you create a string with nothing in it, it is called an empty string. It is normally created by two quote marks with nothing between them.

Some characters that you may want in a string may not exist on the keyboard, or may be special characters that can't appear as themselves in a string. In order to put these characters in a string, you need to use an escape sequence to represent the character. An escape sequence is a character or numeric value representing a character that is preceded by a backslash ( \ ) to indicate that it is a special character.

var home = "c:\\temp";

var quote = "He read \"Ozymandias\" by Percy Bysshe Shelley.";  

Some escape characters are as follows:

Escape Sequence     Character
  \b     Backspace
  \t     Tab. Tabs behave erratically on the Web and are best avoided, but sometimes you need them.
  \v     Vertical tab
  \f     Formfeed
  \n     New line (\u0000a). Inserts a line break at the point specified. It is a combination of the carriage return (\r) and the form feed (\f).
  \r     Carriage return
  \"     Double quote
  \'     Single quote, or an apostrophe, such as in can\'t.
  \\     The backslash, since by itself it is a command.
  \x99     A two digit number specifying the hexadecimal value of a character in the Latin-1 character set (ASCII).
  \u9999     A four digit hecadecimal number specifying a character in the Unicode character set. This is not supported before JavaScript 1.3.

Foreign Characters To display "foreign" characters in html, use the Unicode code. For example, to display a ¿ you type "¿". Example: "¿Qué pasa?"

To write foreign characters using JavaScript you use the \u9999 format specified above, with the 9999 being the hex equivalent of the Unicode character code. For example, the ¿ has a code of 191 (decimal), which when converted to Unicode is BF. You must specify four digits, so you write it as 00BF, or \u00BF. (Converter).

Here is an example

<script>
   document.write("\"\u00BFQu\u00E9 pasa?\"");
</script>

Writes

Section 6: Strings and numbers

JavaScript is very relaxed about the difference between strings and numbers. Some programming languages require you to state if a variable is a number or a string before doing anything else with it. Not so in JavaScript. In fact you can even "add up" numbers and strings:

var string3 = string1 + 12;

Some programming languages would shut down in disgust when they encounter this line. After all, string1 is a string (with the value "Hello world!") and 12 is a number. JavaScript, however, tries to solve the problem by assuming 12 is also a string. So string3 becomes

Hello world!12

So if you use + on a string and a number, JavaScript is going to convert the number to a string for you. Better still, if you need to, you can treat numbers as strings or strings as numbers.

Conversely, if you apply mathematics to a string, JavaScript tries to make it a number. If the string cannot be interpreted as a number (because there are letters in it, for instance), JavaScript responds with NaN (Not a Number).

Finally, JavaScript distinguishes no difference between integers and floating point variables.


Number to String

To convert a number to a string, you can do the following:

var temp = (16 * 24)/49 + 12;

string4 = temp.toString();

Now you can use all string methods on string4, while temp still contains the number.


String evaluation

You can evaluate strings that contain numerals, or even equations, using eval. For example,

var str1 = "2+2";

var num = eval(str1);  // returns the number 4

The use of eval should be reserved for those very rare instances where there is not any alternative way to produce the required result, because improper use of eval opens up your code for injection attacks.


String to Number

As discussed in the Type Conversion notes, if you want to change a string to a number, first make sure there are only the characters 0-9 in the string. The unary + operator (positive sign) type converts its operand to a number, and because it does not do any additional mathematical operations it is the fastest method for type-converting a string into a number.

arrow var numValue = +stringValue;

For example,

var string6 = '1234';

var newNum = +string6;

Since unary + operator assumes numbers, JavaScript makes the string a number, if possible. If that isn't possible, the result is NaN. Note that you will get unexpected results with

var string6 = '1234';

var newNum = string6 + 0;

This would yield '12340' because JavaScript thinks the + means concatenate string, not add.

Section 7: Resources

Supplement: JavaScript data types and data structures/a>

Formatting numbers for decimals and significant digits