Search code examples
javascriptreactjseval

my javascript string is like this: "accepted === accepted && 50 > 100" Now I have to return boolean ans about this string which is valid or not


I am creating one dynamic condition builder and it will return me a javascript string like, tpc_1 === accepted && tpc_6 > 100 After replace function my new string is, accepted === accepted && 50 > 100

Now I have to check if this string is valid as a javascript string or not?

I need the only boolean answer in return. So I decided to use the eval() function. When I passed eval(condition) condition is variable which stores my string it will show me the error like *

accepted is not defined

and when I passed as eval('condition') in the console it will simply be written in the console accepted === accepted && 50 > 100.

So how to solve this issue. Please help me on this.


Solution

  • accepted is considered as variable when eval evaluates. So you have mention string with quotes. Try this eval("'accepted' === 'accepted' && 50 > 100");