Skip to content

Commit 3ff1d8d

Browse files
authored
Fixed console.log output
In modern browsers console.log(1, 3) results in "1 3", not "1,3"
1 parent 93abe18 commit 3ff1d8d

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

scope & closures/ch2.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ function foo(str, a) {
9898

9999
var b = 2;
100100

101-
foo( "var b = 3;", 1 ); // 1, 3
101+
foo( "var b = 3;", 1 ); // 1 3
102102
```
103103

104104
The string `"var b = 3;"` is treated, at the point of the `eval(..)` call, as code that was there all along. Because that code happens to declare a new variable `b`, it modifies the existing lexical scope of `foo(..)`. In fact, as mentioned above, this code actually creates variable `b` inside of `foo(..)` that shadows the `b` that was declared in the outer (global) scope.
105105

106-
When the `console.log(..)` call occurs, it finds both `a` and `b` in the scope of `foo(..)`, and never finds the outer `b`. Thus, we print out "1, 3" instead of "1, 2" as would have normally been the case.
106+
When the `console.log(..)` call occurs, it finds both `a` and `b` in the scope of `foo(..)`, and never finds the outer `b`. Thus, we print out "1 3" instead of "1 2" as would have normally been the case.
107107

108108
**Note:** In this example, for simplicity's sake, the string of "code" we pass in was a fixed literal. But it could easily have been programmatically created by adding characters together based on your program's logic. `eval(..)` is usually used to execute dynamically created code, as dynamically evaluating essentially static code from a string literal would provide no real benefit to just authoring the code directly.
109109

0 commit comments

Comments
 (0)