Why JavaScript shows "undefined" alert in the 1st line of of r() in the below snippet?
Asked
Active
Viewed 199 times
-3

Albert Einstein
- 7,472
- 8
- 36
- 71

Sagar Jain
- 1
- 1
-
please don't share image of your code. – Nihal Apr 24 '18 at 06:08
-
share your code in this post – Nihal Apr 24 '18 at 06:12
2 Answers
1
You need to remove the var from the b in the r function. You cant set a local variable with the same name as global.
function r() {
alert(b);
b = "another stuff";
alert(b);
alert(window.b);
}

Albert Einstein
- 7,472
- 8
- 36
- 71

finnishNinja
- 91
- 1
- 5
-
*"You cant set a local variable with the same name as global."* How sure are you about that statement? – Robby Cornelissen Apr 24 '18 at 06:20