June 2018
Beginner
288 pages
6h 31m
English
At first sight it may appear that JavaScript is highly flexible, but things can get tricky if we’re not careful. Let’s look at the following code as an example:
| 1: | //BROKEN CODE |
| 2: | const oops = function() { |
| 3: | haha = 2; |
| 4: | |
| 5: | console.log(haha); |
| 6: | }; |
| 7: | |
| 8: | oops(); |
| 9: | console.log(haha); |
The function oops assigns a value to a variable haha and then prints it. As expected, the effect of calling oops() will be to print the value 2 to the console. But this function isn’t as benign as it looks. JavaScript looks at line 3 and says, “Hey, look, the developer didn’t explicitly declare the variable before use—what can I do to cause the most damage? Lemme make it global.” Just kidding, no, it’s not ...
Read now
Unlock full access