tags:

views:

82

answers:

6

I have a Javascript String containing true or false.

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

Thanks

A: 
if (str == "true") str_bool = true;
else str_bool = false;
JochenJung
+1  A: 

The most obvious:

if(string =="true"){  
  string = true;  
}else{  
  string = false;  
}

Or using ternary operator:

string = (string=="true") ? true : false;
Ilian Iliev
That's some great use of the ternary operator.....
Motti
A: 

Homework? Steam-driven method:

if (myString == false) myBoolean = false else myBoolean = true

Paul
+1  A: 

If you're using the variable result:

result = result == "true";
clarkf
+6  A: 

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

andrewmu
+4  A: 
val = (string == "true")
Ignacio Vazquez-Abrams