function func(){
var name
console.log(name)
name="pavan"
console.log(name)
}
func()
When I run the above code
it gives me the output undefined pavan.
var name
console.log(name)
name="pavan"
console.log(name)
When I run this modified code it's logging pavan pavan.
What's the difference?
The problem is that you are reusing same variable names when running your experiments.
Consider:
If you use different variable names in both snippets, you will get expected output:
Also note that
nameis a property ofWindowobject. See Window: name property