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
How to "Properly" Create a Custom Object in JavaScript
Passing Data to a Bootstrap Modal
Addeventlistener Calls the Function Without Me Even Asking It To
Array.Prototype.Fill() With Object Passes Reference and Not New Instance
Link Tag Inside Browserrouter Changes Only the Url, But Doesn't Render the Component
How to Parse a Url into Hostname and Path in JavaScript
How to Display a JavaScript Object
How to Force Js to Do Math Instead of Putting Two Strings Together
Getting the Id of the Element That Fired an Event
How to Clone an Array of Objects in JavaScript
What's the Difference Between '$(This)' and 'This'
Fastest Way to Flatten/Un-Flatten Nested JavaScript Objects
Escaping HTML Strings With Jquery
Modifying a Copy of a JavaScript Object Is Causing the Original Object to Change
How to Detect Idle Time in JavaScript
Is There a Cross-Browser Onload Event When Clicking the Back Button