Checking Type of Object


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

'typeof' and 'instanceof' isn't a reliable method to find out the object class, so this is a function that does this in a more trustable way.


Copy this code and paste it in your HTML
  1. function is(type, obj) {
  2. var clas = Object.prototype.toString.call(obj).slice(8, -1);
  3. return obj !== undefined && obj !== null && clas === type;
  4. }
  5.  
  6. is('String', 'test'); // true
  7. is('String', new String('test')); // true

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.