Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use eval in javascript

Shulou Source: shulou.com Published: 2022-06-03 18:43:16 11月12日 Update

Editor to share with you how to use eval in javascript, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The eval function in javascript is used to evaluate a string and executes the JavaScript code in it. The syntax for this function is "eval (string)", and the parameter string represents the string to be evaluated.

This article operating environment: windows7 system, javascript1.8.5 version, DELL G3 computer.

The usage of eval in javascript

The eval () function evaluates a string and executes the JavaScript code in it.

Grammar

Eval (string)

The parameter string must be the string to evaluate that contains the JavaScript expression to evaluate or the statement to execute.

Return value: the value obtained by calculating the string, if any.

Description

This method only accepts the original string as a parameter, and if the string parameter is not the original string, then the method will return without any change. So 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 is no legal expression and statement 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 () passes the exception to the caller.

Tip: although eval () is very powerful, it is rarely used in practice.

Example

In this example, we will apply eval () on several strings and look at the result returned:

Eval ("Xerox 10 / 20; [xss_clean] (Xeroy)") [xss_clean] (eval ("2x / 2")) var x = 10 [XSS _ clean] (eval (x / 17)

Output:

More than 200427 are all the contents of this article entitled "how to use eval in javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Characters strings parameters functions codes properties methods articles original content expressions statements syntax legal powerful not much in use code generation functions most Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft Shulou Information NVidia Shulou Tech Info