I have declared var example at the beginning (it should be in global scope, or?!). Why it is undefined after calling the function?
var example;
function test(){
var x = 2;
var y = 5;
var example = x + y;
console.log(example); // 7: works OK after calling the function
};
test();
console.log(example); // undefined ?!
Edit: Hello, Thanks for your answers, I´ve found an article about this - Variable Shadowing
var example = …inside the function does declare a local variable. Omit thevarand only do the assignment to the global variable.