Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:16:58+00:00 2026-06-12T17:16:58+00:00

Possible Duplicate: JavaScript Variable Scope I am (obviously) pretty new to Javascript, and I’ve

  • 0

Possible Duplicate:
JavaScript Variable Scope

I am (obviously) pretty new to Javascript, and I’ve seen several different points on other threads about the usage of Global and Local variables, but I’m trying to lock down some basic points on the subject in one place.

What is the difference between the following, declared outside (or used inside) of a function?

var thing = 1;

thing = 1;

I understand that using var declares the variable in it’s current scope. So leaving out var makes it global. What are some pitfalls that can show up? Could someone give a simple example of where variables might step on each other in this case?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:17:00+00:00Added an answer on June 12, 2026 at 5:17 pm
    1. If you are in the global scope already, the difference is so small that you don’t have to worry about it

    2. imagine that you had two for loops, and you wanted to do something with them.

    .

    for (i = 0; i < elements.length; i++) {
        element = elements[i];
        for (i = 0; i < items.length) {
            item = items[i];
            element.add(item);
        }
    }
    

    This piece of pseudo-code would work fine in many different languages.
    The program would see an inner-loop and an outer-loop, and could tell that i wasn’t referring to the same thing.

    In JavaScript, they’re the same i.
    That’s because other languages have block-scope — any time you enter new curly braces, the program treats variables like they’re new.

    In JavaScript there is only function scope, meaning that inside of functions variables are treated as new, but inside of control statements (if, for, switch), they’re the same variable with the same value.
    So the outer loop sets i to 0, and then goes into the inner loop.
    The inner loop goes through all of its list of items, and builds i up as it goes…
    Then it goes back to the outer loop, and i still equals items.length - 1…
    If that’s less than elements.length then it adds one to i, which is now higher than items length, so nothing happens in the inner loop, anymore…
    …if items.length is greater than elements.length instead, then the outer loop just ends after one time through.

    Now, I’m sure that you can start to think about times where you might want to use x or name or value or el or sum or i or default or src or url or img or script, etc several times in your whole program (tens of thousands of lines, even), and you can start to think about situations like that loop up above, where things could go wrong if you tried calling two different things by the same name.

    This is the same problem as var-fallthrough

    If you have one function which uses a variable called x and another function which uses another variable called x, that’s great…
    …unless you forget to declare the variable.

    // no problems!
    function func1 () { var x = 0; }
    function func2 () { var x = "Bob"; }
    
    // big problems!
    function func1 () { x = 0; }
    function func2 () { x = "Bob"; }
    

    func1 set window.x = 0;
    func2 set window.x = "Bob";

    …if window.x was supposed to equal 42, for some other program to work right, now you have the potential to have 3 broken apps, just because of a few missing vars.

    It doesn’t instantly set the global variable, though. What it actually does is goes through the function chain. If you create a function inside of another function, then an undeclared var will look to its parent’s vars, and then its grandparent’s vars and then its great-grandparent’s vars…
    If it gets all the way to window and nobody has a var of that name, then it creates one with that name on window.

    function func1 () {
        var x = 0;
    
        function func2 () {
            var y = 1;
            x = 2;
            z = 3;
        }
    
        func2();
    }
    

    When you call func1, it sets its own x to 0, and calls func2.
    func2 sets its own y to 1.
    Then it sets func1’s x to 2.
    Then, because func2 doesn’t have z and func1 doesn’t have z and window doesn’t have z, it sets window.z to 3.

    That’s only the start of the confusion, and why its a very, very good idea to make sure that you’re defining vars which need to be available inside of that function (and in any functions created inside of that function)…
    …and when you reference pre-existing vars, you reference them carefully, and know where that var is supposed to be, in your code (which function defined it… …so where on the chain the program is going to stop looking, before it gets to window), and why you’re changing it from inside of another function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: JavaScript closures and variable scope Assign click handlers in for loop I
Possible Duplicate: Variable scope in Javascript for loop for(i=0;i<4;i++){ } Do I need to
Possible Duplicate: Confused by Javascript's variable scope For example, this is my JavaScript code:
Possible Duplicate: JavaScript Variable inside string without concatenation - like PHP In PHP, double
Possible Duplicate: javascript object, access variable property name? I'm trying to call a method
Possible Duplicate: How to get javascript variable value in php I am looking for
Possible Duplicate: Pass a PHP string to a Javascript variable (including escaping newlines) Hy,
Possible Duplicate: Get variable name. javascript “reflection” Is there a way to know the
Possible Duplicate: Passing javascript variable to PHP Hi I wonder if it's possible to
Possible Duplicate: How to check a not defined variable in javascript Determining if a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.