Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting string "true" / "false" to boolean value [duplicate]

I have a JavaScript string containing "true" or "false".

How may I convert it to boolean without using the eval function?

like image 969
user160820 Avatar asked Oct 20 '22 16:10

user160820


People also ask

How do you change a string from true to boolean true?

To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

How do you convert a string to a boolean in TypeScript?

To convert a string to a boolean in TypeScript, use the strict equality operator to compare the string to the string "true" , e.g. const bool = str === 'true' . If the condition is met, the strict equality operator will return the boolean value true , otherwise false is returned.

How do you convert a value to a boolean?

We convert a Number to Boolean by using the JavaScript Boolean() method. A JavaScript boolean results in one of the two values i.e true or false. However, if one wants to convert a variable that stores integer “0” or “1” into Boolean Value i.e “true” or “false”.


2 Answers

var val = (string === "true");
like image 395
Ignacio Vazquez-Abrams Avatar answered Oct 22 '22 06:10

Ignacio Vazquez-Abrams


You could simply have: var result = (str == "true").

like image 27
andrewmu Avatar answered Oct 22 '22 05:10

andrewmu