Bulletproof type checks in JavaScript

Search for a command to run...

No comments yet. Be the first to comment.
Here are 2 ready-to-use functions to get the first and last millisecond of a day using a few lines of vanilla JavaScript. Get the first moment of a day /** * Get first millisecond of a date * @param {Date} dateObj - JS Date object, default is curre...

Obviously your best guess is to ask the user. That being said, we generally want to avoid interrupting the user. Websites and apps are (or at least should be) about what the user's needs not ours. That being said, given the many legislation requirem...

Skip to solution If you have different language settings in your website or application wouldn't it be great if you could automatically set the language the user prefers? Doing so may be easer than you think. All you have to do is use navigator.langu...

Skip to solution. Problem Knowing your customer location can help you tailor their experience. Other times you may want to check for this information to compile statistics or comply with regulations. What's the best way to get location data? One way...

To check what type is a variable you would most likely use the typeof function. You can even skip the parenthesis (typeof someVariable is same as typeof(someVariable)).
typeof works great for:
typeof "hello"; //string
typeof 1; //number
typeof 0.1; //number
typeof 10000000000000000000000000n; //bigint
typeof Symbol("SomeSymbol"); //symbol
typeof true; //boolean
typeof false; //boolean
typeof function(){}; //function
typeof iamundefined //undefined
It kinda sucks if you need to check:
nullNaN👑 Keep calm and check the solutions below 👍
The use built-in .isArray method will return true/false if the value is an array. This is way better than the old school method of duck typing when you had to check if the variable is an object and has the .length property.
Array.isArray([]); // true
The Number.isInteger method returns true if the variable is an integer.
Number.isInteger(1); //true
Number.isInteger(0.1); //false
Number.isInteger(1000000000000000000000n); //false
Number.isInteger(""); //false
It's a two step process:
// Solution
const isFloat = function isFloat(val){
return (typeof val === 'number' && !Number.isInteger(val));
}
// Test
const floatNumber = 0.1;
console.log(isFloat(floatNumber)); //true
nullBest way is to do a comparison.
const iamnull = null;
const zero = 0;
iamnull === null; //true
zero === null; //false
NaNThe Number.isNaN method returns true if the variable is an integer.
It's an improvement of the older isNaN function, since in that case you should also combine it with typeof check for number, otherwise something like isNaN(undefined) would also return true.
Number.isNaN(NaN); //true
Number.isNaN(undefined); //false
To verify a variable is a regex simply check if it's an instance of the RegExp object.
/I am a regex/ instanceof RegExp //true
true instanceof RegExp //false
Date objectSimilar to regex, for dates we can also check if the value is an instance of the Date object.
new Date() instanceof Date //true
true instanceof Date //false
typeof will return "object" not just for objects but also for things like arrays and null. Your best option is to combine typeof with elimination.
const isObject = function isObject(val) {
return (
typeof val === 'object' &&
!Array.isArray(val) &&
val !== null &&
val instanceof RegExp === false &&
val instanceof Date === false
);
};
You can adjust this based on what you want to qualify as an object, however the objects I would like to validate are non-builtin JavaScript objects aka. objects defined by the developer.