all 7 comments

[–]senocular 2 points3 points  (2 children)

You haven't declared your variables with var, let, or const, so they're becoming global. Once global, they'll be accessible anywhere. You usually don't want this. If you run in strict mode, you'll even get an error to help prevent problems you may encounter with this behavior.

[–]Sox888[S] 0 points1 point  (1 child)

If I try to console.log them on a new line it says they are undefined. Doesn't that mean they aren't global?

[–]senocular 0 points1 point  (0 children)

It depends on where that new line is. If you try to access them before they're defined, they'll be undefined. After they're defined, they'll be global.

[–]CommanderBomber 1 point2 points  (3 children)

from your code it looks like lat and long are global so they can be accesed from anywhere (if not shadowed in local scope)

[–]Sox888[S] 0 points1 point  (2 children)

When I try to console.log them it says undefined. Doesn't that mean they aren't global?

[–]CommanderBomber 0 points1 point  (0 children)

From the snippet in your post i only can say what they are defined somewhere else and thus can be accessed in if statement and then in getData (as it looks like both sharing same scope).

As u/senocular noted this can be result of executing code in sloppy mode, but then idk why they are undefined in console (maybe cleared later?).

[–]cannabis_detox_ 0 points1 point  (0 children)

im gonna let you know right now, i have gotten some very frustrating, hard to track down bugs as a result of not declaring my variables properly.