Convert a JavaScript String to Boolean

Summary: This blog post will teach you how to convert a javascript string to boolean with one easy function. So what does this mean exactly? Well, in javascript there is no Boolean data type, so we need to use strings instead. This can be problematic because sometimes javascript will interpret null or undefined as false which can lead to bugs and errors if not handled correctly.

What is the javascript string to boolean:

The javascript String to the Boolean operator is a handy tool that will convert null, undefined, or false strings into true and vice versa. So how do we make use of it? You just need to include the following at the beginning of your javascript code:

Syntax:

Boolean(value);

Example:

const myString = 'this is my string';
Boolean(string); // true

Explanation:

In the above code, Boolean(string) converts the value of a myString into either true or false using null, undefined, and false as its base.

This how we can convert our string into a boolean.

How can we convert an integer into a boolean?

For converting a number into a boolean you can also use the same method.

Syntax:

Boolean(value)

Return:

True | False

Example:

const myInteger = '10';

Boolean(integer); // false

* Note that javascript string to boolean can be used in the same way for converting a number into a boolean, as demonstrated by the code above.

* Converting true or false strings into integers is just as easy with javascript string to boolean, as demonstrated by the code below:

The False value:

Javascript has many False values like under false, undefined, null, NaN, 0, and (empty string). If you try to convert these values into boolean it will return always False. Unless it will return True.

const myValue1 = false;
const myValue2 = undefined; 
const myValue3 = false
const myValue4 = NaN; 
const myValue5 = 0; 
const myValue6 = ''

Boolean(myValue1 ); // false
Boolean(myValue2 ); // false
Boolean(myValue3 ); // false
Boolean(myValue4 ); // false
Boolean(myValue5 ); // false
Boolean(myValue6 ); // false

There is one more way available in Javascript:

In the way, we just need to put !! in front of the value or variable.

Syntax:

!!variable

Return:

True | False

Example

myvalue = 'my string'
console.log(!!myvalue)

How it works

The single (!) is used to inverse the value like if you have a variable that produces true or false. In that case, you use the (!) the output will turn into vice-versa. So now we know the working of single (!). If you want to reverse it you should use second (!). This is the use of (!!).

Example:

const myValue1 = false;
const myValue2 = undefined; 
const myValue3 = false
const myValue4 = NaN; 
const myValue5 = 0; 
const myValue6 = ''

console.log(!!myValue1) //false
console.log(!!myValue2) //false
console.log(!!myValue3) //false
console.log(!!myValue4) //false
console.log(!!myValue5) //false
console.log(!!myValue6) //false

boolean vs !!:

Both ways are fine to use. we can use any of them. But there is an issue with boolean it is a little bit slower than !!.

What should use?

You have seen both methods are simple and easy to use. But still, we have a question which one we should use? We suggest you can use both. In this situation, I just got confused that what I suggest to you. But in the js community, many developers support boolean. So choose anyone with your wisdom.

Example:

// better (works explicitly)
if (!!a) {
}

// also great (works explicitly)
if (Boolean(a)) {
}

Use any of one is only ur choice there is no difference in between both.

Remember:

Don’t use new keywords use primitives instead of objectstypes.

var myStr = 'str';

// don't use
typeof new Boolean(myStr ); // object

// Preferred
typeof Boolean(myStr ); // boolean
typeof !!myStr ; // boolean

Conclusion:

JavaScript provides a function called Boolean that transforms any String into a Boolean. This is an easy way to convert text-based data into the format needed for mathematical calculations or boolean operations in your code, without having to use complicated if statements or switch cases. 

Also Read : What is appendChild JavaScript?

Follow us on Facebook

Leave a Comment