Check if a value is a boolean in JavaScript
Beginner · 3 min read
Settings screens, API responses and form libraries are full of yes-or-no fields: isActive, darkMode, acceptedTerms. Before your code trusts one of them, it often needs to check that the value really is true or false, and not the string 'true' or the number 1.
In this tutorial you will write isBoolean, a function that answers true only for real booleans. Along the way you will meet a strange object that looks like false but behaves like true. You only need to know what a function is and what if does.
Asking a value what it is
JavaScript has an operator called typeof. Put it in front of any value and it gives back the value's type as a string:
console.log(typeof true); // boolean
console.log(typeof 0); // number
console.log(typeof 'true'); // string
console.log(typeof null); // object
That last line is a famous mistake in the language, and it never got fixed. It doesn't matter for booleans, but keep it in mind: typeof is not always honest.
Type is not the same as truthy
When you write if (value), JavaScript doesn't check for a boolean. It asks whether the value is truthy, meaning it counts as "yes" in a condition. Only a short list of values is falsy: false, 0, '' (the empty string), null, undefined and NaN. Everything else is truthy.
The built-in Boolean() function shows you which side a value falls on:
console.log(Boolean(1)); // true
console.log(Boolean('yes')); // true
console.log(Boolean(0)); // false
console.log(Boolean('')); // false
So 1 is truthy, but it is still a number, not a boolean. Our check has to answer the type question, not the truthy question.
Here is the exercise. Try it on your own first if you like, or read on and we will build it together.
1 Is the value a boolean?
easyWrite a check that is true only for true and false. Not for truthy values, and not for a boolean wrapper object.
Stuck? Three hints, smallest first
- 1.
typeof v === ‘boolean’gets the ordinary cases right. - 2.
new Boolean(false)is an object, and every object is truthy — including that one. - 3.
Object.prototype.toString.call(v)sees through the wrapper.
Step 1: check the type
typeof v === 'boolean' is already true or false, so the function can return it directly:
const isBoolean = (v) => typeof v === 'boolean';
console.log(isBoolean(true)); // true
console.log(isBoolean(false)); // true
console.log(isBoolean(0)); // false
console.log(isBoolean('true')); // false
console.log(isBoolean(null)); // false
console.log(isBoolean(undefined)); // false
isBoolean(false) is true, because false is a boolean. The function asks "is this a boolean?", not "is this true?". That is the whole check for almost every case you will meet.
Step 2: the wrapper object trap
JavaScript also lets you write new Boolean(false). The word new builds an object that wraps the value. That object behaves very differently from the plain false:
const wrapped = new Boolean(false);
console.log(typeof wrapped); // object
console.log(wrapped ? 'yes' : 'no'); // yes
console.log(wrapped.valueOf()); // false
Every object is truthy, including this one. So if (new Boolean(false)) runs the "yes" branch, even though the value inside is false. valueOf() reads the value inside the wrapper.
Nobody writes this on purpose. It usually appears when someone tries to "convert" a value with new Boolean(something). To convert, call Boolean(v) or write !!v, without new:
console.log(typeof Boolean(0)); // boolean
console.log(!!'hello'); // true
console.log(!!''); // false
The good news is that our Step 1 check already refuses the wrapper, because its type is 'object'.
Step 3: a check that sees through the wrapper
Sometimes you have to accept wrappers too. For that there is a trick, often called the tag check. Object.prototype.toString is a built-in function that describes any value as a string like '[object Boolean]':
console.log(Object.prototype.toString.call(true)); // [object Boolean]
console.log(Object.prototype.toString.call(new Boolean(false))); // [object Boolean]
console.log(Object.prototype.toString.call(null)); // [object Null]
.call(v) runs the function on v. The part we want sits between '[object ' (8 characters) and the final ']', so .slice(8, -1) cuts it out:
const tag = (v) => Object.prototype.toString.call(v).slice(8, -1);
const isBooleanLike = (v) => tag(v) === 'Boolean';
console.log(tag(true)); // Boolean
console.log(tag(null)); // Null
console.log(isBooleanLike(new Boolean(false))); // true
console.log(isBooleanLike(1)); // false
The rest of this section uses tag again and again, so it is worth understanding now.
The full solution
The panel has both checks and prints what typeof and the tag say about a wrapped false. Press Run to see the output and the checks.
// The tag trick, used all through this section. It returns a string like
// 'Boolean', 'Null', 'Array' — and unlike typeof it is never wrong.
const tag = (v) => Object.prototype.toString.call(v).slice(8, -1);
// Good enough almost always: rejects wrapper objects too, since typeof
// new Boolean(true) is 'object'.
const isBoolean = (v) => typeof v === 'boolean';
// Accepts wrappers as well, if you have to deal with them.
const isBooleanLike = (v) => tag(v) === 'Boolean';
const wrapped = new Boolean(false);
console.log('typeof true :', typeof true);
console.log('typeof new Boolean() :', typeof wrapped, '<- an object, not a boolean');
console.log('tag(new Boolean()) :', tag(wrapped));
console.log('is new Boolean(false) truthy?', wrapped ? 'yes' : 'no', '<- objects always are');
console.log('so !!wrapped is', !!wrapped, 'while wrapped.valueOf() is', wrapped.valueOf());
Output
11 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 11 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('true is a boolean', isBoolean(true), true);
test('false is a boolean', isBoolean(false), true);
test('0 is not', isBoolean(0), false);
test('the string "true" is not', isBoolean('true'), false);
test('null is not', isBoolean(null), false);
test('undefined is not', isBoolean(undefined), false);
test('a wrapper object is not a real boolean', isBoolean(wrapped), false);
test('but the tag check recognises it', isBooleanLike(wrapped), true);
test('new Boolean(false) is truthy, which is the whole danger', wrapped ? true : false, true);
test('while its actual value is false', wrapped.valueOf(), false);
test('truthiness is a different question from type', isBoolean(1), false);
typeof v === 'boolean' is the right answer here, and the reason to write the exercise anyway is new Boolean(false). It is an object, every object is truthy, so if (new Boolean(false)) runs the true branch. You will not write that on purpose, but code that does new Boolean(someValue) to "convert" it produces exactly this. Use Boolean(v) or !!v to convert — never new.
Common mistakes
- Using
if (v)to check for a boolean. That asks whether the value is truthy.1,'false'and[]all pass it. - Converting with
new Boolean(v). You get an object, and every object is truthy. UseBoolean(v)or!!v. - Comparing with
v == true. Loose equality converts first, so1 == trueistrue. Usetypeof, or===if you really mean the valuetrue.
Try it yourself
- Write
isTrue(v)that returnstrueonly for the booleantrue, and test it with1and'true'. - Write
toBoolean(v)that turns the strings'true'and'false'into real booleans and returnsnullfor anything else. - Use
[true, 0, 'yes', null, false].filter(isBoolean)and predict the result before you run it.