#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Javascript Global Variables


    I am reading "Javascript: Good Parts". In the chapter on the "Awful parts", the author talks about Global Variables:

    "The third is to use a variable without declaring it. This is called implied global:
    foo = value;
    This was intended as a convenience to beginners by making it unnecessary to declare variables before using them. Unfortunately, forgetting to declare a variable is a very
    common mistake. JavaScript’s policy of making forgotten variables global creates bugs that can be very difficult to find."

    My doubt is that: "Does any undeclared variable become a global variable in a javascript script? Or is this applicable to only those undeclared variables that are outside all functions?"
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    variables outside of any function are global by nature (and should thus be avoided). So Crockford refers to variables in functions.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Thanks!


    Originally Posted by Jacques1
    Hi,

    variables outside of any function are global by nature (and should thus be avoided). So Crockford refers to variables in functions.

IMN logo majestic logo threadwatch logo seochat tools logo