1

I've tried this:

function a_function(){   
    var data = "information";      
});
console.log(data);
1
  • A bit more code or further description of what you are trying to achieve would be appreciated. Commented Feb 11, 2016 at 19:32

5 Answers 5

6

You can't. You need to create a variable outside of that scope and assign to it:

var data;
function a_function(){   
    data = "information";      
});
a_function();
console.log(data);

Another answer has been posted mentioning learning about scope. Follow that advice and try to get your head around it early, it will save you a lot of hassle in the future.

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

5 Comments

Seriously, this is programming 101.
I'm feeling generous though - we all have to start somewhere!
You don't need to declare data variable earlier. When you call a_function() data will become global variable automatically.
True, but I would argue that it's bad practice to do it that way
@FixerRB That is an horrendous practice only allowed in non-strict mode because of backwards compatibility reasons. Don't do it.
3

Technically you can, though it's generally not a good idea:

function a_function() {
    data = "information";
}
a_function();
console.log(data);

The reason this works is because, by not using the var keyword to declare a variable, JavaScript (being the good little dynamic language that it is) is writing to the data property of the "current" object, which is window. And, thus, globally accessible. It's basically the equivalent of doing this:

function a_function() {
    window['data'] = "information";
}
a_function();
console.log(window['data']);

As I said, it's generally not a good idea. Other approaches include returning the value:

function a_function() {
    var data = "information";
    return data;
}
var data = a_function();
console.log(data);

Or perhaps creating the value in the larger scope and setting it in the function:

var data;
function a_function() {
    data = "information";
}
a_function();
console.log(data);

Basically, in general you want to maintain scope and control flow and try to avoid "globals" where possible.

Comments

2

you need to read about scope and how it work .

http://www.w3schools.com/js/js_scope.asp

https://en.wikipedia.org/wiki/Scope_%28computer_science%29#JavaScript

For example a variable declared in a function exist only in this function. Once you are outside this fonction, the variable are deleted so it is undefined

Comments

1

You either have to pass the value to a variable in the scope that is shared by the console.log() call like:

var data;
function a_function(){   
    //note the ommission of the 'var' keyword
    data = "information";      
});
a_function();
console.log(data);

or you have to have the function return the value and use that as input to the log() method:

function a_function(){   
    var data = "information";
    return data;
}
console.log(a_function());

or wrap the console.log() call in a helper function that your function calls from within its scope, passing data as an argument:

function log(data){
     console.log(data);
}

function a_function(){   
    var data = "information";
    log(data);
}

EDIT: as others have mentioned, while we can tell you all the ways to do this, it is better to learn more about scope and understand why the methods people suggest work.

Comments

1

If you want to ecapsulate data but use it outside of the function a_function, you need to return data and call a_function. Something like this should work:

function a_function() {
  var data = "information";
  return data;
};

var data = a_function();

Note, due to scope, data inside the function is different than data outside the function. If you don't create a new var named data outside the function, you will not be able to access data because it doesn't exist in the outer scope.

The following code demonstrates scope:

function a_function() {
  var data = "information";
  return data;
};

var data = "different information";
var data2 = a_function();
console.log(data);
console.log(data2);

The output from the code will yield:

different information
information

Comments

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.