Didn't declare a variable part II


#1

Hi all,

In this post I wrote about a program seemingly running as intended despite my not having declared a variable (with var, anyway). (I'd have added on to that thread, but it's closed.)

Just now, in some recommended reading (MDN), I saw the following:

You can also use delete to delete a global variable if the var keyword was not used to declare the variable:

g = 17;
delete g;

I tested it with the following program:

g = 300;
console.log(g);     // 300

delete g;
console.log(g);     // ReferenceError: g is not defined

Would it then be correct to infer that delete variableName can be used as an alternative to refreshing the browser to get rid of world-dominating variables?

Also, outside of this situation, when would delete variable be a good idea? I've tried searching this myself, but the results I saw mostly seemed to be (outdated?) debates regarding whether this is possible, and or how, but not when, to do so.

To my nubly eyes, it seems like it would be preferable to remove an unnecessary variable from the program itself rather than to declare one, then delete it, and, again through my nubvision™ with my nubbily short programs, it seems like space taken up by a variable is theoretically not a big dill. Any feedback regarding either or both questions would be greatly appreciated.

Thanks all!
frisby


https://www.codecademy.com/courses/javascript-beginner-en-NhsaT/1/3?curriculum_id=506324b3a7dffd00020bf661


Replace this line with your code.


#2

Yea, you can get rid of them. But you can always overwrite a variable as well:

g = "test"
g = "something else"

actually, to understand memory management better, learning C is a good choice. This will give you valuable inside into memory management.

i am not sure about that. Now you have 3 steps: declaring a variable, delete it, and create a new one. If you just overwrite the old one, you already have allocated memory, and it is a step less (2 steps instead of 3)


#3

Hi @stetim94, thank you for your help!

I read several times that it is "bad" to declare variables without var but wasn't sure why (aside from the fact that they are global, but surely sometimes you need a global variable). Also, if I understand correctly, all variables declared outside of functions are global...

Would it be correct to say the warning against declaring variables without 'var' is less about declaring global variables and more about avoiding accidentally creating a global variable where one intends to make a local one? [1]

actually, to understand memory management better, learning C is a good choice. This will give you valuable inside into memory management.

I thought if I were to study another language after JS and Ruby it'd be Python (I've always liked the name) or something like Clojure (I recently watched/read some things from Destroy All Software talks/Douglas Crockford that made it sound appealing). Also, as a nub, I felt like there are so many "C" named languages....
Thank you for your recommendation; since I have had several questions regarding memory management and frequently see comparisons of JS to C and C++ (which are currently incomprehensible to me) when I google my JS questions, I will seriously consider taking up C upon getting the hang of JS.

i am not sure about that. Now you have 3 steps: declaring a variable, delete it, and create a new one. If you just overwrite the old one, you already have allocated memory, and it is a step less (2 steps instead of 3)

I'm unsure if I was unclear but if I was I apologize. I meant that I felt instead of using the delete function it would be better to just remove the variable from the code itself and find a way to work around it if necessary. My thinking was that if I needed to delete it for some reason, perhaps that would be treating the "symptom" of an excess variable, where as the true problem would be inefficient code.


#4

I am not up to date yet with the latest js standards (es6), but a variable outside a function/object is always global. Even with var keyword.

yea, i think it is. Also, you can't delete variable with a var keyword, so you can't accidentally delete them.

But python and ruby are both (IMHO) high level language, pick a low one (C), this will deepen your understanding of all the questions you have.

Yea, you can remove a variable. But when it is efficient, or might cause problems depends up on the case, difficult question to answer


#5

Thank you for your help, stetim.

I did read a debate (Guss' responses on the first answer) regarding the issue of var keyword variables, although I can't claim to understand what the conclusion is on it (perhaps that they can be deleted in a very particular way).

But python and ruby are both (IMHO) high level language, pick a low one (C), this will deepen your understanding of all the questions you have.

I mention Ruby only because I was doing that track before JS but switched for a test. And Python...I just like the name :slight_smile: It seems "bada__" compared to others, haha. In terms of practical, irl, decisions though, I meant to communicate that I will look into learning C after JS as per your suggestion.

Yea, you can remove a variable. But when it is efficient, or might cause problems depends up on the case, difficult question to answer

Perhaps one day I will become a successful programmer and, upon finding just such a situation and will look back on these times with nostalgia and think "so that's what that was."

Thank you again for your help and advice, stetim! I greatly appreciate it.