JavaScript
Type Checking
Precisely determine JavaScript data types.
By EZ4Code Team
typecheck
Code
function getType(val) {
return Object.prototype.toString.call(val).slice(8, -1).toLowerCase();
}
const is = {
array: v => Array.isArray(v),
object: v => getType(v) === "object",
function: v => typeof v === "function",
string: v => typeof v === "string",
number: v => typeof v === "number" && !isNaN(v),
promise: v => getType(v) === "promise"
};Explanation
Uses Object.prototype.toString to precisely determine various data types.
More JavaScript Snippets
Array Map Filter Reduce
Chain map, filter, reduce, find, some, and every on arrays.
Array Deduplication
Deduplicate an array using Set.
Deep Clone
Deep clone objects, supporting common data types.
Debounce Function
Wait a period of time after an event triggers before executing; reset the timer if triggered again during the wait.
Throttle Function
Limit a function to execute at most once within a time interval.
Promise.all Concurrency Control
A Promise executor with concurrency limit.