I have a string that contains Boolean logic something like:
var test = "(true)&&(false)&&!(true||true)"
What is a good way to evaluate this string in JavaScript to get the boolean value of false in this case
- I know we could use eval() or new Function().. - but is that a safe approach?
- I am guessing the other option would be to write a custom parser. Being a fairly new person to JS, would that be a lot of effort? I could not find any examples of parsers for Boolean logic expressions
- Any other alternatives?
"(true)&&(false)&&!(true||true)"?evalonly when you can guarantee the source isn't malicious and doesn't contain an issue. If you're the one building the string and you've written your code to guarantee only a boolean string is constructed, it should be fine."(1)&(0)&!(1||1)"(all tokens are only 1 character), it would be easier to parse