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

  • Home
  • SEARCH
  • 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 8358359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:46:11+00:00 2026-06-09T10:46:11+00:00

var a=1; //first one function x() { var a=2; // second function y() {

  • 0
 var a=1;   //first one
 function x() {
    var a=2;  // second
    function y() {
       var a=3;  // third one
    }
 }

Is there any way that function y() can access the second var a? I know it can access first one via window.a.

  • 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-09T10:46:12+00:00Added an answer on June 9, 2026 at 10:46 am

    As-written?
    No.

    If you’re not dead-set on naming each one a, then you can easily reference it.
    The other solution would be to capture the outside variable within another variable, the trick being to not have the same variable name being referenced in the outside scope, from the inside scope.

    window.a = 1;
    function x() {
        var a = 2,
            inner_a = a,
    
            y = function () {
                var old_a = inner_a,
                // a is equal to the closest var assignment ie: inside x()
                    a = 3;
            };
     }
    

    or to pass it into the construction of a new function through closure
    (immediately-invoking function)

    window.a = 1;
    function x() {
        var a = 2;
        var y = (function (old_a) {
            return function () { var a = 3; }; 
            // this inner function has access to "old_a", through closure
        }(a));
    }
    

    This is a pattern that is preferred for several use-cases, when mixing JS with browser-functionality (ie: the DOM and DOM events, loops which assign timers or callbacks, AJAX responses, et cetera).

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

Sidebar

Related Questions

I have two dates: var first = '21-11-2012'; var second = '03-11-2012'; What is
I have two dates in javascript: var first = '2012-11-21'; var second = '2012-11-03';
First I create some backbone views: (function() { var SomeView = Backbone.View.extend({ ... });
I want to know if there is a way use jQuery (in a chaining
In my view, there are 5 dropdownlists. The first one is populated when the
I declared two variables globally, first one is input text field and second is
I am extracting a character in a Javascript string with: var first = str.charAt(0);
First i run this (in chrome): var start; var end; start = (new Date()).getTime();
First,I created a hidden frame like this: var oHiddenFrame = null; if(oHiddenFrame == null){
First, I was calling my functions like this: var a=1; a=foo(a,2); Then I discovered

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.