Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SerkanSipahi/f504f36716cf9160b2ee to your computer and use it in GitHub Desktop.
Save SerkanSipahi/f504f36716cf9160b2ee to your computer and use it in GitHub Desktop.
A Pen by Bitcollage.
<h2>nice way to check type of:</h2>
<ul>
<li>number</li>
<li>string</li>
<li>function</li>
<li>object</li>
<li>boolean</li>
<li>regexp</li>
<li>array</li>
</ul>
<h3>If you use jQuery in your project and want also use "nice way to check type of", please make sure to init it after jQuery loaded!</h3>

Nice type checking in javascript :)

Type checking, detection in javascript. Number, String, Function, Object, Boolean, Regexp

A Pen by Bitcollage on CodePen.

License.

//=============== Global Init =================
Object.prototype.type = function(){
return Object.prototype.toString.call(this).slice(8,-1).toLowerCase();
};
//======================================
// easy way to check type of number, string, etc... !
var foo1 = 123456,
foo2 = 'Hello World',
foo3 = function(){},
foo4 = {},
foo5 = true,
foo6 = /[a-zA-Z]/,
foo7 = [1,3,4,5];
console.log(foo1.type()); //numer
console.log(foo2.type()); //string
console.log(foo3.type()); //function
console.log(foo4.type()); //object
console.log(foo5.type()); //boolean
console.log(foo6.type()); //regexp
console.log(foo7.type()); //array
// whenever you want just call .type() :)
h2 { font-family:arial; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment