Testing Type

Contact Us or call 1-877-932-8228
Testing Type

Testing Type

JavaScript offers a way to test the "type" of a variable. However, the result can be confusing -- for example, the type of an Array is "object".

It's common practice to use the typeof operator when trying to determining the type of a specific value.

Testing the Type of Various Variables

var myFunction = function() {

var myObject = {
	foo : 'bar'

var myArray = [ 'a', 'b', 'c' ];
var myString = 'hello';
var myNumber = 3;
var nothing;

typeof myFunction;   // returns 'function'
typeof myObject;     // returns 'object'
typeof myArray;      // returns 'object' -- careful!
typeof myString;     // returns 'string';
typeof myNumber;     // returns 'number'
typeof nothing;      // returns 'undefined;

typeof null;         // returns 'object' -- careful!

jQuery offers utility methods to help you determine the type of an arbitrary value. These will be covered later.