May 14th, 2020
Finding type of data in JS
img

JS is not a strictly typed language. So its not really easy to find out ways to say what type the input is. I will keep this post like a cheatsheet to find out all data types.

Copy 📋
// string
let isString = (inp) => typeof inp === "string";

// boolean
let isBoolean = (inp) => typeof inp === "boolean";

// number
let isNumber = (inp) => typeof inp === "number";
// isNumber(NaN) = true
// isNumber(Infinity) = true

// number
let isWholeNumber = (inp) => isNumber(inp) && inp % 1 === 0;

// Array
let isArray = (inp) => typeof inp === "object" && inp.constructor === Array;

// Object
let isObject = (inp) => typeof inp === "object" && inp.constructor === Object;