bitsmcgee77
bitsmcgee77

Reputation: 391

Using 'new Boolean()' to evaluate logical expressions

How can I evaluate (to true or false) a logical expression stored in a var as a string, without using eval()?:

var test = '(1 < 2 || 2 < 1) && 1 < 0)'

Upvotes: 0

Views: 430

Answers (1)

guest271314
guest271314

Reputation: 1

var test = '((1 < 2 || 2 < 1) && 1 < 0)';
console.log(new Boolean(new Function(`return ${test}`)()));

Upvotes: 3

Related Questions