3. For or while


#1

When I declare a variable in this exercise, I noticed that I didn't need to use the keyword 'var'. Why is that? Is that only a Codecademy feature or is that universal outside of this website?

For example, this while loop still runs even though x = 5 does not have a 'var' in front of it.

x = 5; <--no 'var' here!
while(x){
console.log(x);
x = 0;
}

and even in the Introduction to 'While' Loops in JS lesson, Section 3. A fellow of infinite loops, I found in this exercise:

understand = true; <-- no 'var' here!

while(understand){
console.log("I'm learning while loops!");
//Change the value of 'understand' here!
understand = false;
}


#2

using the keyword var will create a local variable, which can't be used outside the function, look at the really simple example:

function myFunction(){
    var x = 3;
    y = 5;
}
myFunction();
console.log(y)
console.log(x);

console.log(y) will print 5 because y is a global variable, console.log(x) will give you a error, since x is a local variable (it only exist inside the function). So, the keyword var is crucial for variable scope


#3

In addition to what @stetim94 already mentioned maybe have a look at this:

Also is your title in any way related to your question?


#4

Hi @haxor789 - Thank you for responding and sorry I wasn't clear about the title. I used the section where my example came from but I will try to be clearer next time.

I read through what you put down and also @stetim94 response but I still don't understand why sometimes 'var' is used and sometimes it isn't. I know global variables can be seen in all codes and local variables only inside a particular function. I am not sure what I don't understand, but I put down both codes and where I found them. Maybe if someone can explain the comparison examples it will help? idk.

In Intro to While Loops, Section 2, 'var' was used:

var understand = true; <--- 'var' is used

while(understand){
console.log("var understand = true is a statement that is true ;understand = false is a statement that evaulates to false");
understand = false;
}

and then in Intro to While Loops, Section 3: 'var' is not used:

understand = true; <-- 'var' is not used here.

while(understand){
console.log("I'm learning while loops!");
//Change the value of 'understand' here!
understand = false;
}


#5

In this case it doesn't matter, both are global variable. it does matter when you don't want to assign a value, you need the var keyword:

var x;

there might more to it, i am still learning js, there is a lot to learn


#6
understand = true; <-- 'var' is not used here. 

while(understand){
	console.log("I'm learning while loops!");
	//Change the value of 'understand' here!
    understand = false;
}

There should be a var as well. As a rule of thumb I'd suggest that whenever you use a variable for the first time declare it by using var.

What this does is requesting space for a value and limiting the scope of the variable. And as tempting as global variables might be on first look, if you're programs get bigger you it gets really messy if you use global variables and it is also a huge security issue if you can access everything from everywhere. So again a rule of thumb "Make things as global as necessary and as local as possible".

And if you really want to use global variables have a look at the stack overflow link and because just using name = value is probably the least best option better attach it to the window object or just use var on an higher scope.

Hope this helps if not please be a bit more specific for what you want to know this?