├── typeOf.js └── README.md /typeOf.js: -------------------------------------------------------------------------------- 1 | /** typeOf.js | Reliable JavaScript Type-checking Utility 2 | * @author Oyedele Hammed Horlah 3 | * @version 1.0 4 | * @since January 21, 2018 5 | * @see https://github.com/Horlahcoded/typeOf.js 6 | */ 7 | 8 | function typeOf( value ) { 9 | return {}.toString.call( value ).slice( 8, -1 ).toLowerCase(); 10 | } 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # typeOf.js 2 | 3 | typeOf.js is a lightweight and efficient JavaScript Type-checking utility. 4 | Since, the `typeof` operator in JavaScript is not reliable, i created this utility that efficiently tells the Data Type of a value. 5 | 6 | # Examples 7 | Below are type-checking examples of common types: 8 | 9 | ```javascript 10 | typeOf( "Hello world" ); // "string" 11 | typeOf( 5 ); // "number" 12 | typeOf([ 1, 2, 3 ]); // "array" 13 | typeOf({ a: 1, b: 2, c: 3 }); // "object" 14 | typeOf( function(){} ); // "function" 15 | typeOf( new Date() ); // "date" 16 | typeOf( true ); // "boolean" 17 | typeOf( /.*/ ); // "regexp" 18 | typeOf( new Error() ); // "error" 19 | typeOf( null ); // "null" 20 | typeOf(); // "undefined" 21 | typeOf( new Symbol() ); // "symbol" 22 | typeOf( function* gen(){} ); // "generatorfunction" 23 | ``` 24 | 25 | Thanks. 26 | Oyedele Hammed Horlah --------------------------------------------------------------------------------