The boolean is a primitive type in Typescript. It represents a simple true/false value. They are implemented as numerical values with a single binary digit (i.e., 0 & 1). The Boolean is an object wrapper for the primitive boolean value. In this tutorial, Let us find out more about boolean data type and the difference between Boolean
object and boolean
primitive. The Typescript also has a Boolean global function, which we use to convert any value to a boolean a primitive type. We also learn how to convert boolean to string and string to boolean. We also learn about truthy and falsy values in typescript.
Table of Contents
Defining boolean
There are two ways you can create the primitive boolean
variable. using the normal variable declaration or using the global Boolean
function.
boolean declaration
The following example creates two primitive boolean
variables. You can see that the typeof
returns as boolean
.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | let isDone: boolean = true; // primitive boolean type let isPending:boolean = false; // primitive boolean type console.log(isDone); //output true console.log(isPending); //output false console.log(typeof(isDone)); //output boolean **** output **** true false boolean |
Boolean Global function
The Boolean()
global function converts its argument to a primitive boolean value and returns it.
1 2 3 4 5 6 7 8 9 | let boolvar=Boolean(true) console.log(boolvar) //true console.log(typeof(boolvar)) //boolean **** output **** true boolean |
You can pass any value to the Boolean
function. It always converts them to either true
or false
. Whether a particular value converts to true
or false
depends on whether the value is truthy
or falsy
.
Truthy & Falsy in TypeScript
We can convert any type to boolean
in Typescript using the Boolean
function
Falsy are those values, that convert to boolean
false. There are eight falsy
values in Typescript. They are false
, 0
(zero), -0
(minus zero) , 0n
(BigInt zero) , " "
(empty string), null
, undefined
& NaN
.
Everything else converts to true, hence we call them Truthy.
Examples of Falsy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | console.log(boolvar) boolvar=Boolean(0) // 0 is false console.log(boolvar) boolvar=Boolean(-0) // -0 is also false console.log(boolvar) boolvar=Boolean(0n) //BigInt 0 is also false console.log(boolvar) boolvar=Boolean("") //empty string is false console.log(boolvar) boolvar=Boolean(null) //null is also false console.log(boolvar) boolvar=Boolean(undefined) //undefined is also false console.log(boolvar) boolvar=Boolean(NaN) //NaN is also false console.log(boolvar) |
Examples of truthy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | let boolvar:boolean boolvar=Boolean(true) //true is true console.log(boolvar) boolvar=Boolean("test") //string console.log(boolvar) boolvar=Boolean("0") //0 as string console.log(boolvar) boolvar=Boolean("false") //false as string console.log(boolvar) boolvar=Boolean(100) //number other than 0 & -0 console.log(boolvar) boolvar=Boolean({}) //a emty object console.log(boolvar) let employee = { name:'test'} boolvar=Boolean(employee) // non empty object console.log(boolvar) boolvar=Boolean([]) //a emty array console.log(boolvar) boolvar=Boolean(new Date()) //a emty array console.log(boolvar) boolvar=Boolean(Infinity) //Infinity both +ve & -ve console.log(boolvar) |
Boolean Object
The Boolean
is an object and is a wrapper around boolean
.primitive type. You can create a Boolean object using the constructor.
1 2 3 4 5 6 7 8 9 10 | let boolVar = new Boolean("test"); console.log(boolVar); console.log(typeof(boolVar)); ***output*** [Boolean: true] object |
As shown in the previous example, zero, empty string, null, undefined results in false. Everything else returns true.
You can use the valueOf
method to get the primitive boolean
back.
1 2 3 4 5 6 7 8 9 | let boolobj=new Boolean("test") console.log(boolobj) //[Boolean: true] console.log(typeof(boolobj)) //object console.log(boolobj.valueOf()) //true console.log(typeof(boolobj.valueOf())) //boolean |
Boolean vs boolean
The boolean
is a primitive type and Boolean
is an object. Boolean()
is a global function
The boolean
is created using variable declaration or using the Boolean
() function. The Boolean
object is created using the constructor (using the new Boolean()
)
And since the Boolean
object is an object, and comparing it with a boolean value always results in true, irrespective of its internal value.
1 2 3 4 5 6 7 8 9 10 11 | var b = new Boolean(false); console.log(b.valueOf()) //false console.log(typeof b) //object if (b) { console.log("b is true although b is false"); } |
Convert boolean to number
Converting the boolean to the number primitive will result in 1 for true and 0 for false.
1 2 3 4 5 6 7 8 9 10 | let trueNum=Number(true) let falseNum=Number(false) console.log(trueNum); // 1 console.log(falseNum); // 0 console.log(typeof(trueNum)) //number console.log(typeof(falseNum)) //number |
Convert boolean to string
1 2 3 4 5 6 7 8 9 10 | let trueStr=String(true); let falseStr=String(false); console.log(trueStr) //true console.log(falseStr) //false console.log(typeof(trueStr)) //string console.log(typeof(falseStr)) //string |
Summary
The boolean is a primitive type in Typescript. The Typescript also has the Boolean
object. Always use the primitive boolean. When converting the other data types to boolean remember that zero, empty string, null, undefined, NaN returns false. Everything else returns true.
References
Read More