addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1linklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

Global Variables in Javascript

From: Wil Moore I.
Sent on: Monday, May 18, 2009 12:59 AM
At the last meetup, several attendees were curious as to the problems global variables could potentially cause if used carelessly in Javascript. Others wanted to know how one went about defining a global variable assuming one has a choice.

As will be shown in my example, one does have a choice.

See my example posted to JSBin:
NOTE: view the source code of the page and check out the code between the < script > and < / script > tags

A few things to note -- the code will run immediately upon page load so read through the output shown in the huge textarea. You should then read the source code and the comments. If you are wondering why the functions execute without actually being called by name, notice the () after the function declaration.

I hope this is useful to someone,

-Wil Moore III

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy