JavaScript typeof and JavaScript Data Types

By:    Updated: January 23,2017

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
<script type="text/javascript">
var site;
var site_name = "valinv";
var site_online = true;
var site_age = 2;
var site_init = function()
	alert("Welcome to Valinv");
var site_slogan = null;

alert(typeof site); //undefined
alert(typeof site_name); //string
alert(typeof site_online); //boolean
alert(typeof site_age); //number
alert(typeof site_init); //function
alert(typeof site_slogan); //object

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:

<script type="text/javascript">
var site;
var site_name = undefined;

alert(site == undefined);  //true
alert(site_name == undefined); //true

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".

<script type="text/javascript">
var site_slogan = null;
alert(typeof site_slogan); //object

alert(null == undefined); //true, using == between null and undefined always returns true

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.

<script type="text/javascript">
var site_online1 = true; //or site_online1 = false;
var site_online2 = True; //not as boolean value

alert(Boolean(site_online1)); //true

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.

<script type="text/javascript">
var fs_age1 = 28;   //decimal
var fs_age2 = 034;  //octal, a zero (0) followed by a sequence of octal digits (0 to 7)
var fs_age3 = 0x1c; //hexadecimal, 0x followed by any number of hexadecimal digits (0 to 9, and A to F ). A - F may be in uppercase or lowercase.

alert(fs_age1); //28
alert(fs_age2); //28
alert(fs_age3); //28

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.

<script type="text/javascript">
var fs_age1 = 28.1;
var fs_age2 = 0.1;
var fs_age3 = .1; //valid, but not recommended

var fs_age4 = 28.; //convert it into integers: no digit after the decimal point
var fs_age5 = 28.0; //convert it into integers: whole number

var fs_memory1 = 3.2e10; //using e-notation: equal to 32000000000, take 3.2 and multiply it by 10^10
var fs_memory2 = 3.2e-1; //equal to 0.32

alert(NaN == NaN);      //false
alert(isNaN(NaN));      //true
alert(isNaN(28));       //false - 28 is a number
alert(isNaN("28"));     //false - can be converted to number 28
alert(isNaN("valinv")); //true - cannot be converted to a number
alert(isNaN(true));     //false - can be converted to number 1

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.

<script type="text/javascript">
var fs_channel = 10;

// parseInt(variable, radix = 10);

parseInt(fs_channel, 2); //2 - parsed as binary
parseInt(fs_channel, 8); //8 - parsed as octal
parseInt(fs_channel, 10); //10 - parsed as decimal
parseInt(fs_channel, 16); //16 - parsed as hexadecimal

// parseFloat(variable);
var num1 = parseFloat("2014valinv"); //2014, returns an integer when no decimal point or only a zero after the decimal point
var num2 = parseFloat("0xA");        //0, hexadecimal numbers always become 0.
var num3 = parseFloat("1.1");        //1.1
var num4 = parseFloat("11.22.33");   //11.22, the rest of the string after second decimal point will be ignored
var num5 = parseFloat("0007.1");     //7.1
var num6 = parseFloat("3.2e10");     //32000000000

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:

<script type="text/javascript">
//A string using double quotes is exactly the same as a string using single quotes in ECMAScript.
var fs_site1 = "valinv";
var fs_site2 = 'valinv';

Character Literals

The String data type includes several character literals to represent nonprintable or otherwise useful characters, as listed in the following table:

Literal    Meaning
\n    New line
\t    Tab
\r    Carriage return
\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:

<script type="text/javascript">
var fs_welcome = "Hello,"; //First, defined to "Hello,"
fs_welcome = fs_welcome + "everybody";//Second, redefined: creating a new string with enough space for 18 characters to filled with "Hello,everybody". Last, destroy the original string "Hello," and the string "everybody"

//The length of any string can be returned by using the length property
alert(fs_welcome.length); //15

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).

<script type="text/javascript">
//Using on common string
var fs_welcome = "Hello,"; 
var fs_welcome_string = fs_welcome.toString(); //the string "Hello,"
//on boolen
var fs_online = true;
var fs_online_string = fs_online.toString(); //the string "true"

//on numbers, syntax: variable.toString(radix = 10);
var fs_age = 28;
var fs_age_string_10 = fs_age.toString(10);
var fs_age_string_2 = fs_age.toString(2);

//If a value is null or undefined, toString is not available, so use String() casting function
var fs_slogan = null;
var fs_ads;

alert(String(fs_slogan)); //the string "null"
alert(String(fs_ads)); //the string "undefined"

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:

  • constructor
  • toString()
  • hasOwnProperty(propertyName)
  • isPrototypeOf(object)
  • propertyIsEnumerable(propertyName)
  • toLocaleString()
  • valueOf()


More in Development Center
New on Valinv
Related Articles
Sponsored Links