Find type of variable


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function type(o){
  2. return !!o && Object.prototype.toString.call(o).match(/(\w+)\]/)[1];
  3. }
  4.  
  5. // or this way too
  6.  
  7. (function(){
  8. var types = ['Array','Function','Object','String','Number'],
  9. typesLength = types.length;
  10. while (typesLength--) {
  11. window['is' + types[typesLength]] = (function(type){
  12. return function(o) {
  13. return !!o && ( Object.prototype.toString.call(o) === '[object ' + type + ']' );
  14. }
  15. })(types[typesLength]);
  16. }
  17. })();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.