How can I check if a variable\'s type is of type Boolean?
I mean, there are some alternatives such as:
if(jQuery.type(new Boolean()) === jQuery.type(
Sometimes we need a single way to check it. typeof not working for date etc. So I made it easy by
Date.prototype.getType() { return "date"; }
Also for Number
, String
, Boolean
etc. we often need to check the type in a single way...
You can use pure Javascript to achieve this:
var test = true;
if (typeof test === 'boolean')
console.log('test is a boolean!');
if(['true', 'yes', '1'].includes(single_value)) {
return true;
}
else if(['false', 'no', '0'].includes(single_value)) {
return false;
}
if you have a string
val === false || val === true
.typeof variable == typeof true
.The shortest, but not readable at all: !!val === val
.
Explanation:
Tests: