How to Convert a String to Boolean in JavaScript

How can I convert a string to boolean in JavaScript?

Do:

var isTrueSet = (myValue === 'true');

using the identity operator (===), which doesn't make any implicit type conversions when the compared variables have different types.

This will set isTrueSet to a boolean true if the string is "true" and boolean false if it is string "false" or not set at all.



Don't:

You should probably be cautious about using these two methods for your specific needs:

var myBool = Boolean("false");  // == true

var myBool = !!"false"; // == true

Any string which isn't the empty string will evaluate to true by using them. Although they're the cleanest methods I can think of concerning to boolean conversion, I think they're not what you're looking for.

Convert string to Boolean in javascript

I would use a simple string comparison here, as far as I know there is no built in function for what you want to do (unless you want to resort to eval... which you don't).

var myBool = myString == "true";

Converting string true / false to boolean value

var val = (string === "true");

How can I convert a string to boolean in JavaScript?

Do:

var isTrueSet = (myValue === 'true');

using the identity operator (===), which doesn't make any implicit type conversions when the compared variables have different types.

This will set isTrueSet to a boolean true if the string is "true" and boolean false if it is string "false" or not set at all.



Don't:

You should probably be cautious about using these two methods for your specific needs:

var myBool = Boolean("false");  // == true

var myBool = !!"false"; // == true

Any string which isn't the empty string will evaluate to true by using them. Although they're the cleanest methods I can think of concerning to boolean conversion, I think they're not what you're looking for.

How to convert string to Boolean in javascript?

You can use JSON.parse('true');

JSON.parse(isShow.toLowerCase());

Try the example below.

var result = ['True', 'False']

var isShow = result[Math.round(Math.random())];

console.log(JSON.parse(isShow.toLowerCase()));

Can't convert string to boolean

I don't know how the ini works, but did you check what is the type of value?

Maybe it has already parsed it as a boolean and that is why you get false when comparing?

"true" == true // false

How to convert string to boolean in typescript Angular 4

Method 1 :

var stringValue = "true";
var boolValue = (/true/i).test(stringValue) //returns true

Method 2 :

var stringValue = "true";
var boolValue = (stringValue =="true"); //returns true

Method 3 :

var stringValue = "true";
var boolValue = JSON.parse(stringValue); //returns true

Method 4 :

var stringValue = "true";
var boolValue = stringValue.toLowerCase() == 'true'; //returns true

Method 5 :

var stringValue = "true";
var boolValue = getBoolean(stringValue); //returns true
function getBoolean(value){
switch(value){
case true:
case "true":
case 1:
case "1":
case "on":
case "yes":
return true;
default:
return false;
}
}

source: http://codippa.com/how-to-convert-string-to-boolean-javascript/



Related Topics



Leave a reply



Submit