Created
April 19, 2014 13:06
-
-
Save jasdeepkhalsa/11083919 to your computer and use it in GitHub Desktop.
Type checking in JavaScript (better than using typeOf)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function checkType(obj) | |
| { | |
| var type = Object.prototype.toString.call(obj); | |
| switch(type) | |
| { | |
| case '[object Number]': | |
| return 'number'; | |
| case '[object String]': | |
| return 'string'; | |
| case '[object Boolean]': | |
| return 'boolean'; | |
| case '[object RegExp]': | |
| return 'regexp'; | |
| case '[object Function]': | |
| return 'function'; | |
| case '[object Array]': | |
| return 'array'; | |
| case '[object Object]': | |
| return 'object'; | |
| case '[object Date]': | |
| return 'date'; | |
| case '[object Math]': | |
| return 'math'; | |
| case '[object Null]': | |
| return 'null'; | |
| case '[object Undefined]': | |
| return 'undefined'; | |
| case '[object global]': | |
| return 'global'; | |
| default: | |
| return 'unknown'; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment