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?

4
  • If you don't want that variable local to that function, define it outside the function... Commented Sep 13, 2015 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? Commented Sep 13, 2015 at 12:46
  • It was an interview question, any way to make it available in the outer scope on some condition. Commented Sep 13, 2015 at 12:49
  • 1
    You can make it available to the outer scope but then it wouldn't be local anymore. Commented Sep 13, 2015 at 12:50

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

Looking for any answer without changing 'var bar = 10'. It was an interview question.
Then it's impossible...you'd have to assign that value to a variable already declared in the global scope...
@Nimmy Testing your confidence levels :) You should say NOT POSSIBLE sir ;)
@Suresh atta :) Yeah I did, but just wanted to check if I was right,
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.
|
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?

Comments

1

You can do something like this:

var myScopedVariables = function foo(){
    var bar = 10;
    return [bar];
} 
console.log(myScopedVariables);

1 Comment

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.
0
function myFunction() { myVar = 'Hello'; // global variable } myFunction(); console.log(myVar); // output: "Hello"

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.