2. Variable Scope

Scope is like oxygen to a programmer. It’s everywhere. You often don’t even think about it. But when it gets polluted . . . you choke.

The good news is that JavaScript’s core scoping rules are simple, well designed, and incredibly powerful. But there are exceptions. Working effectively with JavaScript requires mastering some basic concepts of variable scope as well as the corner cases that can lead to subtle but nasty problems.

Item 8: Minimize Use of the Global Object

JavaScript makes it easy to create variables in its global namespace. Global variables take less effort to create, since they don’t require any kind of declaration, and they are automatically accessible to all code throughout the program. This convenience makes ...

Get Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.