1

I am trying to evaluate an expression like a == b ? 'Ok' : 'Cancel'; using JavaScript eval() but is shows error.

Is there any way to execute and return string out of eval() ?

Regards, Nwbrd

1 Answer 1

3

Your error is likely related to a or b being not defined prior to calling eval(), for ex: the following code works fine with the eval() call returning a string as expected.

var a = 5, b = 6;
var result = eval("a == b ? 'SAME' : 'DIFFERENT'");
alert(result);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.