I have a JavaScript string containing "true" or "false".
How may I convert it to boolean without using the eval function?
I have a JavaScript string containing "true" or "false".
How may I convert it to boolean without using the eval function?
var val = (string === "true");
Case insensitive:
const val = (string.toLowerCase() === "true");
var before the declaration of val.You could simply have: var result = (str == "true").
result = result === "true"; Here the parentheses are unnecessary, but should check the type and value.If you're using the variable result:
result = result == "true";
result = result === "true"; Here the parentheses are unnecessary, but should check the type and value.