3

Is there any way to make local scope variable accessible in outer scope without creating an object or without using 'this'?

Say,

function foo(){
  var bar = 10;

}

Any way to make variable 'bar' available outside function 'foo' scope?

nim007
  • 2,958
  • 3
  • 16
  • 28
  • If you don't want that variable local to that function, define it outside the function... – Andy Sep 13 '15 at 12:44
  • 1
    It doesn't make much sense to me. Local scope is designed to be local why would you want it to be part of any other scope? – MinusFour Sep 13 '15 at 12:46
  • It was an interview question, any way to make it available in the outer scope on some condition. – nim007 Sep 13 '15 at 12:49
  • 1
    You can make it available to the outer scope but then it wouldn't be local anymore. – MinusFour Sep 13 '15 at 12:50

5 Answers5

7

No. Simply you can't. Scope is scope. If you want to access outside, make it declare outside and use it.

That's how things designed. I don't suggest any crappy way to make it possible.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

Assign the value to a property of the window object:

function foo(){
    window.bar = 10;
}

console.log(window.bar); //10

EDIT:

Since you can't accept his answer, then no - what you're asking for is impossible. Only solution is to declare the variable in the global scope, then initialize it later.

jonny
  • 3,022
  • 1
  • 17
  • 30
  • 1
    Looking for any answer without changing 'var bar = 10'. It was an interview question. – nim007 Sep 13 '15 at 12:45
  • 1
    Then it's impossible...you'd have to assign that value to a variable already declared in the global scope... – jonny Sep 13 '15 at 12:45
  • 1
    @Nimmy Testing your confidence levels :) You should say NOT POSSIBLE sir ;) – Suresh Atta Sep 13 '15 at 12:47
  • 1
    @Suresh atta :) Yeah I did, but just wanted to check if I was right, – nim007 Sep 13 '15 at 12:50
  • 3
    Actually, if this was an interview question, then @sᴜʀᴇsʜᴀᴛᴛᴀ's answer re is _exactly_ what they were looking for. The important word here that you should explain and then enhance on is "scope", not just say that it's not possible. They're looking for someone who understands why _and_ can explain it. So blah blah unlike other languages JavaScript has function scope rather than block scope and local variable aren't available outside their functions, although the new version of JS (ES2016 or whatever it's called now) also has block scope etc. – Andy Sep 13 '15 at 12:51
  • 1
    @Andy Very well observed - if it's impossible, your employers want to know that you understand the *if* and *why* it's impossible – jonny Sep 13 '15 at 12:58
2

You can't access local variable outside the function.

Following post might help you to understand scopes in more detail -

What is the scope of variables in JavaScript?

Community
  • 1
  • 1
Avinash T.
  • 2,280
  • 2
  • 16
  • 23
1

You can do something like this:

var myScopedVariables = function foo(){
    var bar = 10;
    return [bar];
} 
console.log(myScopedVariables);
  • This is the answer. Any value from within a function that should be accessed outside that function should be returned by that function. Simple as that. – Jamesfo Oct 12 '22 at 17:22
0

function myFunction() { myVar = 'Hello'; // global variable } myFunction(); console.log(myVar); // output: "Hello"

obed
  • 1
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '23 at 23:27