Skip to content

Commit 70cf2b2

Browse files
committed
minor
1 parent 329a53c commit 70cf2b2

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

10-misc/02-eval/article.md

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Eval: run a code string
22

3-
The built-in `eval(code)` function allows to execute a string of `code`.
3+
The built-in `eval` function allows to execute a string of `code`.;
4+
5+
The syntax is:
6+
7+
```js
8+
let result = eval(code);
9+
```
410

511
For example:
612

@@ -56,46 +62,46 @@ Without `use strict`, `eval` doesn't have its own lexical environment, so we wou
5662

5763
## Using "eval"
5864

59-
In modern programming `eval` is used very sparingly. There's also an expression "eval is evil".
65+
In modern programming `eval` is used very sparingly. It's often said that "eval is evil".
6066

61-
The reason is simple: long, long time ago JavaScript was a weak language, many things could only be done with `eval`. But that time has passed.
67+
The reason is simple: long, long time ago JavaScript was a much weaker language, many things could only be done with `eval`. But that time passed a decade ago.
6268

63-
Right now, there's almost no reason to use `eval`. If someone is using it, there's a good chance they can replace it with a modern language construct, or [JavaScript Modules](info:modules).
69+
Right now, there's almost no reason to use `eval`. If someone is using it, there's a good chance they can replace it with a modern language construct or a [JavaScript Module](info:modules).
6470

65-
Still, if you're sure you need `eval`, please note that its ability to access outer variables has side-effects.
71+
Still, if you're sure you need to dynamically `eval` a string of code, please note that its ability to access outer variables has side-effects.
6672

67-
Code minifiers (tools used before JS gets to production, to compress it) replace local variables with shorter ones. That's safe, unless `eval` is used. When they see `eval`, they thing it might use local variables, so they don't replace all local variables that might be visible from `eval`. That negatively affects code compression ratio.
73+
Code minifiers (tools used before JS gets to production, to compress it) replace local variables with shorter ones for brewity. That's usually safe, but not if `eval` is used, as it may reference them. So minifiers don't replace all local variables that might be visible from `eval`. That negatively affects code compression ratio.
6874

69-
Also, renaming a local variable becomes more dangeours overall.
75+
Using outer local variables inside `eval` is a bad programming practice, as it makes maintaining the code more difficult.
7076

71-
Using outer variables inside `eval` is a bad programming practice.
77+
There are two ways how to evade any eval-related problems.
7278

73-
There are two solutions.
79+
**If eval'ed code doesn't use outer variables, please call `eval` as `window.eval(...)`:**
7480

75-
**If you don't use outer variables, please call `eval` as `window.eval(...)`:**
81+
This way the code is executed in the global scope:
7682

7783
```js untrusted refresh run
78-
let a = 1;
84+
let x = 1;
7985
{
80-
let a = 5;
81-
window.eval('alert(a)'); // 1
86+
let x = 5;
87+
window.eval('alert(x)'); // 1 (global variable)
8288
}
8389
```
8490

85-
**If your code needs variables, execute it with `new Function`:**
91+
**If your code needs local variables, execute it with `new Function` and pass them as arguments:**
8692

8793
```js run
8894
let f = new Function('a', 'alert(a)');
8995

9096
f(5); // 5
9197
```
9298

93-
The `new Function` construct is explained in the chapter <info:new-function>. It creates a function from a string. Local variables can be passed to it as parameters, like in the example above.
99+
The `new Function` construct is explained in the chapter <info:new-function>. It creates a function from a string, also in the global scope. So it can't see local variables. But it's so much clearer to pass them explicitly as arguments, like in the example above.
94100

95101
## Summary
96102

97-
- A call to `eval(code)` runs the code and returns the result of the last statement.
98-
- Rarely used in modern JavaScript.
103+
A call to `eval(code)` runs the string of code and returns the result of the last statement.
104+
- Rarely used in modern JavaScript, as there's usually no need.
99105
- Can access outer local variables. That's considered bad practice.
100-
- To execute the code in the global scope, use `window.eval(code)`.
101-
- If your code needs some data from the outer scope, use `new Function` and pass it as arguments.
106+
- Instead, to `eval` the code in the global scope, use `window.eval(code)`.
107+
- Or, if your code needs some data from the outer scope, use `new Function` and pass it as arguments.

0 commit comments

Comments
 (0)