CyrefJS
CCyrefJS
فا
Back to documentation
Type/Since 1.38.0

isBoolean

Checks whether a value is a boolean and provides a TypeScript type guard for boolean narrowing.

isBoolean(value):value is boolean

Implementation

The function uses JavaScript's typeof operator and checks whether the result is exactly "boolean".

Type guard

The return type `value is boolean` makes the function a TypeScript type guard. When the function returns true, TypeScript can narrow the checked value to boolean.

Boolean primitive

The function recognizes only the boolean primitive values true and false. Boolean wrapper objects are not recognized as boolean values.

Parameters

valueunknownrequired

The value to check.

Returns

value is boolean

Returns true when the value is a boolean; otherwise returns false. When true, TypeScript narrows the value to boolean.

Examples

01

Basic usage

Check whether a value is a boolean.

TypeScript
isBoolean(true);
// true

isBoolean(false);
// true

isBoolean("true");
// false
02

Type narrowing

The type guard narrows an unknown value to boolean inside the conditional block.

TypeScript
const value: unknown = true;

if (isBoolean(value)) {
  value.valueOf();
  // value is narrowed to boolean
}
03

Other primitive values

Numbers, strings, null, undefined, and objects are not considered booleans.

TypeScript
isBoolean(1);
// false

isBoolean("false");
// false

isBoolean(null);
// false

isBoolean(undefined);
// false

isBoolean({});
// false
04

Boolean objects

A Boolean object is not considered a boolean primitive because typeof returns "object".

TypeScript
isBoolean(new Boolean(true));
// false
Built with@cyref/js·Official documentation