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 8034795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:07:41+00:00 2026-06-05T02:07:41+00:00

I know global variables are created when they are declared outside a function (says

  • 0

I know global variables are created when they are declared outside a function (says W3Schools).

If I create a global variable and edit it in a function, does it become local? Does the new value given by the function become the global value?

  • 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-05T02:07:42+00:00Added an answer on June 5, 2026 at 2:07 am

    In general, no, editing a global does not make it local:

    var myglob = 5;
    function incGlob() {
        myglob = myglob + 1;
    }
    
    incGlob();
    console.log(myglob); // is 6 now
    

    However, if you pass the global variable as an argument, the argument is a local copy:

    var myglob = 5;
    function incArg(myloc) {
        myloc = myloc + 1;
    }
    
    incArg(myglob);
    console.log(myglob); // is still 5
    

    Note that objects are passed by reference, so editing the member variables of an argument variable changes the member variables of the original object passed in:

    var myglob = { foo:5 };
    function editLoc(myloc) {
        myloc.foo = 6;
    }
    
    editLoc(myglob);
    console.log(myglob.foo); // foo is 6 now
    

    Finally, note that the local variable in editLoc, above, is just a reference. If we try to overwrite the entire object (instead of a member variable), the function simply loses the reference to the original object:

    var myglob = { foo:5 };
    function clobberLoc(myloc) {
        myloc = { bar:7 };
    }
    
    clobberLoc(myglob);
    console.log(myglob.foo); // myglob is unchanged...
    // ...because clobberLoc didn't alter the object,
    // it just overwrote its reference to the object stored in myglob 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I should avoid using global variables in the first place due to
I know that, in Delphi, instance variables and global variables are initialized to zero
I know how to do a global variable, but whenever I try to define
As I know, I can use global variable transfer the value among the multi
I know global variables are supposed to be bad but is it possible to
after reading 'What is the best way to create global variables in CakePHP?' in
im trying to create global variables of wx.Panel, wx.Button, and wx.TextCtrl, so that i
I created a global variable: MPMoviePlayerController *player; I play the video with the following
It's known that big local/global variables may cause to a stack overflow. I know
I want to create an alias of @param for global variables used within 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.