0

Possible Duplicate:
How can I execute Javascript stored as a string?

Let us suppose this:

var a = 10;
var b = 20;
var exp = "a+b";
var result = ?;

Change the above expression which is string to give result in integer. Variable a is 10. Variable b is 20. Variable exp is a string containing the expression. Change that string expression to add variable a and variable b and store it in Variable result.

How can I do that with Javascript?

Community
  • 1
  • 1
Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62

1 Answers1

1

By using Javascript eval() function you can do above thing.

Like:

var a = 10;
var b = 20;
var exp = "a+b";
var result = eval(exp);
Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62