Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript Type Stuff
// ============================================================
// Objects vs Primitives
// To make a long story short, use primitives wherever you can.
// ============================================================
// Primitive types: Null, Undefined, Number, Boolean, String.
var num1 = 9000,
num2 = new Number( 9000 ),
str1 = "hello world",
str2 = new String( "hello world" );
num1 // 9000
num2 // 9000
num1 === num2 // false !?
typeof num1 // "number"
typeof num2 // "object" !?
str1 // "hello world"
str2 // "[object Object]" !?
str2 + "" // "hello world"
str1 === str2 // false !?
str1 === str2 + "" // true
// ============================================================
// The typeof Operator
// A few things you should probably be aware of.
// ============================================================
typeof 1 // "number"
typeof "amazing" // "string"
typeof true // "boolean"
typeof {} // "object"
typeof [] // "object" ?!
typeof new Number( 1 ) // "object" ?!
typeof new String( "yo!" ) // "object" ?!
typeof new Boolean( true ) // "object" ?!
typeof function(){} // "function"
typeof /^foo$/i // "function" ?!
typeof undefined // "undefined"
typeof null // "object" ?!
typeof NaN // "number" ?!
// ============================================================
// The instanceof Operator
// A few more things you should probably be aware of.
// ============================================================
var obj = {}; // sample object
function fn(){} // sample function
obj instanceof Object // true
[] instanceof Array // true
fn instanceof Function // true
/^bar$/i instanceof RegExp // true
1 instanceof Number // false ?!
"amazing" instanceof String // false ?!
true instanceof Boolean // false ?!
Array( 1, 2, 3 ) instanceof Array // true
Function( "return 1" ) instanceof Function // true
RegExp( "^bar$", "i" ) instanceof RegExp // true
Object() instanceof Object // true
Number( 1 ) instanceof Number // false ?!
String( "amazing" ) instanceof String // false ?!
Boolean( true ) instanceof Boolean // false ?!
Date( "1/1/1999" ) instanceof Date // false ?!
new Number( 1 ) instanceof Number // true
new String( "amazing" ) instanceof String // true
new Boolean( true ) instanceof Boolean // true
new Date( "1/1/1999" ) instanceof Date // true
new Array( 1, 2, 3 ) instanceof Array // true
new Function( "return 1" ) instanceof Function // true
new RegExp( "^bar$", "i" ) instanceof RegExp // true
new Object() instanceof Object // true
// And, for what it's worth...
obj instanceof Object // true
[] instanceof Object // true
fn instanceof Object // true
/^bar$/i instanceof Object // true
1 instanceof Object // false
"amazing" instanceof Object // false
true instanceof Object // false
Array( 1, 2, 3 ) instanceof Object // true
Function( "return 1" ) instanceof Object // true
RegExp( "^bar$", "i" ) instanceof Object // true
Object() instanceof Object // true
Number( 1 ) instanceof Object // false
String( "amazing" ) instanceof Object // false
Boolean( true ) instanceof Object // false
Date( "1/1/1999" ) instanceof Object // false
new Number( 1 ) instanceof Object // true
new String( "amazing" ) instanceof Object // true
new Boolean( true ) instanceof Object // true
new Date( "1/1/1999" ) instanceof Object // true
new Array( 1, 2, 3 ) instanceof Object // true
new Function( "return 1" ) instanceof Object // true
new RegExp( "^bar$", "i" ) instanceof Object // true
// ============================================================
// Smarter Type Checking
// Endorsed by the professionals!
// ============================================================
// Since every Object has .toString, and all types inherit from Object...
var obj = {},
arr = [1,2,3];
obj.toString // "[object Object]"
arr.toString // "1,2,3"
// Use Object .toString method explicitly with call.
var toString = Object.prototype.toString;
toString.call( 1 ) // "[object Number]"
toString.call( Number( 1 ) ) // "[object Number]"
toString.call( new Number( 1 ) ) // "[object Number]"
toString.call( "a" ) // "[object String]"
toString.call( String( "a" ) ) // "[object String]"
toString.call( new String( "a" ) ) // "[object String]"
// A basic type-checking function.
function type( obj ) {
var types = {
"[object Number]": "number",
"[object String]": "string",
"[object Boolean]": "boolean",
"[object Function]": "function",
"[object Array]": "array",
"[object Date]": "date",
"[object RegExp]": "regexp",
"[object Object]": "object"
};
return types[ Object.prototype.toString.call( obj ) ];
}
type( 1 ) // "number"
type( Number( 1 ) ) // "number"
type( new Number( 1 ) ) // "number"
type( "a" ) // "string"
type( String( "a" ) ) // "string"
type( new String( "a" ) ) // "string"
@cowboy

This comment has been minimized.

Copy link
Owner Author

commented Feb 16, 2011

Yep. This actually comes from a slide deck I have on "JavaScript Gotchas." I just wanted to share the contents with people who might want it.

@devi

This comment has been minimized.

Copy link

commented Feb 16, 2011

Thanks for this. Now I don't have to re-open the manual when I forgot this kind of things.

@jdalton

This comment has been minimized.

Copy link

commented Feb 16, 2011

Here is some spec to shed some light on the instanceof stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.