the functions to check the variant type in javascript


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

to check the variant type in javascript. it's useful.


Copy this code and paste it in your HTML
  1. function isAlien(a) {
  2. return isObject(a) && typeof a.constructor != 'function';
  3. }
  4.  
  5.  
  6. function isArray(a) {
  7. return isObject(a) && a.constructor == Array;
  8. }
  9.  
  10. function isBoolean(a) {
  11. return typeof a == 'boolean';
  12. }
  13.  
  14. function isEmpty(o) {
  15. var i, v;
  16. if (isObject(o)) {
  17. for (i in o) {
  18. v = o[i];
  19. if (isUndefined(v) && isFunction(v)) {
  20. return false;
  21. }
  22. }
  23. }
  24. return true;
  25. }
  26.  
  27. function isFunction(a) {
  28. return typeof a == 'function';
  29. }
  30.  
  31. function isNull(a) {
  32. return typeof a == 'object' && !a;
  33. }
  34.  
  35. function isNumber(a) {
  36. return typeof a == 'number' && isFinite(a);
  37. }
  38.  
  39. function isObject(a) {
  40. return (a && typeof a == 'object') || isFunction(a);
  41. }
  42.  
  43. function isString(a) {
  44. return typeof a == 'string';
  45. }
  46.  
  47. function isUndefined(a) {
  48. return typeof a == 'undefined';
  49. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.