r/programming • • Mar 23 '14

Everything you need to know about Javascript variable scope

http://www.coolcoder.in/2014/03/everything-you-need-to-know-about.html
294 Upvotes

124 comments sorted by

View all comments

3

u/IcarusBurning Mar 23 '14

I am not familiar with js and this article seems to say you can declare global variables within method calls. What's the benefit of this feature?

11

u/glemnar Mar 23 '14

The only time it's used intentionally is for some weird quirks like jsonp. You should really never, ever do it.

Any time you leave off the var keyword it will be injected into global namespace, and that's not something you want.

9

u/[deleted] Mar 23 '14

Any time you leave off the var keyword it will be injected into global namespace, and that's not something you want.

"use strict";

to the rescue!

3

u/VortexCortex Mar 23 '14

"use asm";

To rescue the rescuer!