Accurate Type Checking
Accurate Type Checking is a reusable JavaScript snippet. Reliable type checking in JavaScript that correctly distinguishes between arrays, objects, null, and other types. The full implementation: function getType(value) { return Object.prototype.toString.call(value) .slice(8, -1) // "[object Array]" -> "Array" .toLowerCase() } // Specific checks const is = { array: v => Array.isArray(v), object: v => v !== null && typeof v === 'object' && !Array.isArray(v), string: v => typeof v === 'string', number: v => typeof v === 'number' && !Number.isNaN(v), null: v => v === null, undefined: v => v === undefined, function: v => typeof v === 'function', date: v => v instanceof Date, promise:… This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.