I've tried this:
function a_function(){
var data = "information";
});
console.log(data);
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.
data variable earlier. When you call a_function() data will become global variable automatically.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.
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
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.
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