Converting string "true" / "false" to boolean value

JavascriptBoolean

Javascript Problem Overview


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

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

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

Solution 3 - Javascript

If you're using the variable result:

result = result == "true";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser160820View Question on Stackoverflow
Solution 1 - JavascriptIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 2 - JavascriptandrewmuView Answer on Stackoverflow
Solution 3 - JavascriptclarkfView Answer on Stackoverflow