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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:19:38+00:00 2026-06-17T23:19:38+00:00

I have a global var defined in a .js file.: If my function has

  • 0

I have a global var defined in a .js file.:

If my function has an argument with the same name as the global variable, the function can not initialise the global variable:

var myvar;

function init(myvar){
    myvar= myvar;
}

But if I change the variable names so the global variable and argument have different names, the function can initialise the global variable:

var myvar2;

function init(myvar){
    myvar2= myvar;
}

(myvar2 is then initialised correctly).

Why is this the case ? javascript does not allow two variables with same name in different scope so variable shadowing is occuring ?

I thought the js engine would be be able to distinguish between method scope and global scope in the same way that java does (albeit using the this keyword)

  • 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-17T23:19:39+00:00Added an answer on June 17, 2026 at 11:19 pm

    That is correct; the global variable is being shadowed. Think about it. In this statement:

    myvar = myvar;
    

    if there was no shadowing, which myvar would be the global one and which the local one?

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

Sidebar

Related Questions

For example, I have $theme global variable that is defined in the configuration file,
In Actionscript you can have global variables like this: var number : Number =
I have a js file test.js that contains a global variable global_var and a
I have a global variable contacts, var contacts = ContactsApp.getContacts(); I have an KeyUphandler
I have many js methods that uses global variables like: function DoSomething() { var
I have a global js file which has an ajax script. which is included
In my mvc4 project, I have the following routing defined in Global.asax: routes.MapHttpRoute( name:
Can Dynamic libraries have global, local and static variables in linux? If yes, then
If i have global variable in A.dll, that depends on global variable in B.dll
I have a global JSON variable where I store some parameters and then each

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.