Firstly I want to say that I was searching some information about scope variables in javascript (e.g What is the scope of variables in JavaScript?) and I was trying to understand why my code behaves like that but finally I really don't know. I can't override var a in my function in function, I can do this only using a = 6;
without var. Why a is undefined before if statement and overriding this variable ? Why there is no result like this:
5
equal 5
equal 6
I have something like this instead:
undefined
not equal 5
equal 6
Here's my code:
var a = 5;
function something(){
console.log(a);
if(a == 5){
console.log('equal 5')
}
else{
console.log('not equal 5');
}
var a = 6;
function test(){
if(a == 6){
console.log('equal 6');
}
else{
console.log('not equal 6')
}
}
test();
}
something();