Here is a good explanation on youtube:
If you like things being visual, then that seems like the place to go. Look for other more advanced vidoes as you go.
Scope can be a little confusing to a beginner. To make matters worse, JS does a few nonstandard things that other languages don’t. As to your questions:
Scope has to do with where a variable is “visible”. That has to do with where it is defined. Not everything is visible everywhere.
var a = 10; // outer level of code, global, visible, everywhere
console.log(a) // outputs "10", no problem.
function myFunc() {
console.log(a) // outputs "10", no problem. this function is a "child" of the global scope so anything visible there is visible here
var b = 12; // defined inside the function so that is it's scope
}
console.log(b); // undefined, the b from the function is out of scope here
for ( i=0; i <10; i++) {
console.log(a); // outputs "10", no problem
}
console.log(i); // outputs 10 (what i is now) because for loops are part of the scope where the originate
This is some basics. Things can get more complicated. (Much more complicated.) As to your questions.
Reads “variables fall out of scope” - understand scope and variables, but confused by the falling bit - off to google
Fall is just a metaphor. JS keeps track of what the scope is for each allocated memory block. As long as we are in that scope or a child of it, it is still “in scope”, when we return to a parent of that scope, it is “out of scope.” For example, in the above example, when we call myFunc, it allocates memory for b. When we leave the function, that memory is no longer needed so it is freed up. If we call the function again, new memory is allocated from scratch.
first answer “variables are out of scope when not within the scope of the function”
That’s basically what I just described. Once we leave the function b is out of scope and has no meaning. If we need that data, we either have to declare b globally (global variables are kind of “cheating” and can be dangerous if done too much) or return a value.
googles more - finds answer that references “JavaScript garbage collection when variable goes out of scope” (goes out = falling?)
Yes, they are the same thing. It’s just a different metaphor.
follows link, sort of makes sense but refers to “nongenerational mark-and-sweep garbage collector,” “closures and event handlers,” and “freeing objects.”
OK, that’s a little outside my ken. I think a garbage collector is just what frees up unused memory. Closures are an extremely important JS topic, but you have to understand scope first. There will be some excellent YT videos on it. Event handlers just handle asynchronous events - a mouse is clicked so the program jumps to a certain function to handle it. I have no idea what freeing objects are.
Make a note to go back to Memory leaks… realizes posts were from 2007, wonders if memory is even an issue considering Moore’s law …
There should be a corollary to Moore’s Law (if there isn’t already) that no matter how dense, cheap, and abundant memory gets, programs will always find ways to use it up. So yes, memory leaks will always be an issue. On a little web page with some images and a few click handlers? Not so much. But it will be an issue on any program that tries to use the system to its fullest abilities. It crops up on games all the time because they are always trying to push the limits. No matter how much memory people pack into a computer, someone will write a program that tries to use all of it.
But don’t get frustrated. These are confusing topics. Keep at it. And I’ve found youtube to be a great resource for these types of things. Just type “javascript scope” or “javascript closures”. It may take a few tries but you’ll get it. I feel like I understand how JS uses scope now. I’m almost there with closures …