Is it possible to pass a string into an if statement as a means to create dynamic if statements? A simplified example would be:
var condition = "bool == true";
if(condition) console.log('It is true!);
Of course the example above does not work, because it always returns true, because the string has content. Is there some way to inject to contents of condition into the if statement as actual parameters though? The condition needs to be in string format because in a real world application, the condition will be pulled out of an existing HTML block using a regular expression and is therefore returned in string format.
Vanilla javascript only please.
evalwould work, but it’s dodgy.How confident are you that these strings are always going to be valid javascript?
What happens if someone tricks your app into putting
(or worse) into that HTML block?
If you find yourself needing to use
evalit generally means you should rethink your approach.