I have following javascript that I came across. I don't understand the process flow of the code execution.
var val = 'ap';
function func() {
if (!val) {
var val = 'ple';
}
console.log(val);
}
func();
console.log(val);
The output that I thought would be was, 'ap' then 'ap'. But I get 'ple' and then 'ap'. How is this happening?