Is it possible to convert/wrap an array value into a if statement? For clarification, the statement I received from my code is always a string in an array, and I want to execute it in an if block. Right now, it does not do a comparison because it is a string. So how can I convert a string into a statement that will be compared?
let counter = 11;
let statement = ['counter > 10']; // array string received from other code (always a string)
if(statement[0]) {
// should be true
}
Result expected to be true, where the if statement is dynamically executed.
EDIT: I found an answer by Using Function:
Other answers and comments has mentioned
eval
- obviously the most dangerousnew Function
- easy to pollute global scopeIf you really want to execute any code, isolated-vm
may help. (only in Node.js)
Also, when using isolated-vm
, restricting the permission of the process is a must.