TomRed.net

  • Increase font size
  • Default font size
  • Decrease font size
Subscribe Bookmark and Share
Home Tutorials CSS JavaScript typeOf Operator

JavaScript typeOf Operator

User Rating: / 0
PoorBest 

typeOf

 

The typeof operator is used in either of the following ways:

  1. typeof operand
  2. typeof (operand)

The typeof operator will return a value relating to the type of object passed as the operand. A table is outlined below defining what this might be. If you click the test button you see the output of the Sample column beside it. In these cases I have used the parentheses but these are optional.

Type Result Sample Test
Undefined "undefined" var undef;
alert(typeof(undef));
Null "object" alert(typeof(null));
Boolean "boolean" alert(typeof(true));
Number "number" alert(typeof(2));
String "string" alert(typeof('test'));
Function object "function" var myFun = new Function("test");
alert(typeof(myFunction));
Any other object "object" var myArr = new Array();
alert(typeof(myArr));

Some examples of functions

typeof Date == 'function'
typeof Function == 'function'
typeof Math == 'object'
typeof Object == 'function'
typeof String == 'function'