The eval(String) function can calculate a string and execute the JavaScript code inside it.
Return value
The value obtained by calculating string (if any).
illustrate
This method only accepts the original string as argument, and if the string parameter is not the original string, the method will return without any changes. Therefore, please do not pass a String object as an argument to the eval() function.
If you try to override the eval property or assign the eval() method to another property and call it through that property, the ECMAScript implementation allows an EvalError exception to be thrown.
Throw out
If there are no legal expressions and statements in the parameter, a SyntaxError exception is thrown.
If eval() is called illegally, an EvalError exception is thrown.
If the Javascript code passed to eval() generates an exception, eval() will pass the exception to the caller.
Tips and comments
Tip: Although eval() has very powerful functions, it is not often used in actual use.
example:
<html><body><script type="text/javascript">eval("x=10;y=20;document.write(x*y)")document.write("<br />")document.write(eval("2+2"))document.write("<br />")var x=10document.write(eval(x+17))document.write("<br />")eval("alert('Hello world')")</script></body></html>Output:
200
4
twenty four