In this code the I expect it to print I love Java
. But var
is function scoped. For this reason it is hoisted to top and saw that it's defined. So I'd expect the answer to be JavaScript
but wit outputs Java
, why?
var lang1 = 'Java'
var lang2 = 'JavaScript'
function getLanguage(){
if(!lang2){
var lang2 = lang1
}
return lang2
}
console.log(`I love ${getLanguage()}`)