Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
boolean
communitycreator

What is Boolean.toString() in JavaScript?

Theodore Kelechukwu Onyejiaku

The toString() method of a Boolean class in JavaScript returns a boolean value as a string. This means that it returns true or false as a string.

Syntax

boolean.toString

Parameter

No parameter is needed. This method is only called on the boolean value.

Return value

This method returns true or false, depending on the value of boolean.

Code

In the example below, we create boolean variables in an ordinary way and by using the Boolean constructor. Then, we log out the value when the toString() method is called on it.

/*
  create ordinary boolean values
  and by boolean object
*/ 
let a = true                // true
let b = false               // false
let c = new Boolean(true)   // true
let d = new Boolean(false)  // false
let e = new Boolean(1)      // true
let f = new Boolean(0)      // false 
let g = new Boolean("hello")// true
let h = new Boolean ("")    // false

// log string values to console
console.log(a.toString())
console.log(b.toString())
console.log(c.toString())
console.log(d.toString())
console.log(e.toString())
console.log(f.toString())
console.log(g.toString())
console.log(h.toString())

RELATED TAGS

javascript
boolean
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring