Home >Web Front-end >JS Tutorial >How to use the eval function in JavaScript and its risks

How to use the eval function in JavaScript and its risks

不言
不言Original
2018-12-24 14:12:085524browse

The eval function can calculate a certain string and execute the How to use the eval function in How to use the eval function in JavaScript and its risks and its risks code in it. So what is the specific usage of the eval function? This article will introduce to you the usage and security performance of the eval function in How to use the eval function in How to use the eval function in JavaScript and its risks and its risks.

How to use the eval function in How to use the eval function in JavaScript and its risks and its risks

Let’s first take a look at the basic syntax of the eval function

eval(string)

Put the string like this, therefore, How to use the eval function in How to use the eval function in JavaScript and its risks and its risks code can be passed around freely and manipulated.

Let’s look at a specific code

document.write(eval("1+1")+"<br>");
var k=10;
document.write(eval(k+20)+"<br>");

The first line uses the string "1 1" as How to use the eval function in How to use the eval function in JavaScript and its risks and its risks code, and displays the calculation result as "2", and the " at the end of the line The
" part is used to wrap lines when displayed.

The second line puts the number "10" in the variable k. Here, we have specified the string k 20 as a parameter, but actually the calculated result "30" is displayed as How to use the eval function in How to use the eval function in JavaScript and its risks and its risks code.

In the code at this time, the calculation formula of the number passed as a string is processed as a program to be calculated as a numerical value or variable of the string through the eval function.

The results of running the above code are as follows

How to use the eval function in How to use the eval function in JavaScript and its risks and its risks

Finally let’s take a look at the risks of using the eval function

Using the eval function has the following risks

1. Malicious How to use the eval function in How to use the eval function in JavaScript and its risks and its risks code prepared by a third party can also be executed at will.

2. The execution speed is lower than that of ordinary How to use the eval function in How to use the eval function in JavaScript and its risks and its risks programs, and the performance decreases

So, in fact, we generally do not use the eval function, and most of the processing now can be executed without the eval function.

This article ends here. For more exciting content, you can pay attention to the relevant columns of the php Chinese website! ! !

The above is the detailed content of How to use the eval function in JavaScript and its risks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn