convert to bool if "false" or "true", leave string otherwise

Advertisements

What is the most efficient one-liner (if possible) for:

if (val == "true"){
 value = true;
}
else if (val == "false"){
 value = false;
}
else {
 value = val;
}

val is string.

>Solution :

Do it in one line with ternary operator :

value = val == "true" ? true : (val == "false" ? false : val);

Leave a Reply Cancel reply