Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Aliayub Ali
3,495 PointsHelp!!!! with while and do loops or just while.
When I am creating while loop, Do I have to always declare the variables outside of the while loops or inside the while loops curly brackets, same for the do loop and for loops. like this.
- while(ayub<10){ var x=2; ayub++; console.log(ayub)
} does this happens in real life coding anyway it doesn't matter if the code is not working or not
- do{ var x=0; x++; console.log(x) }while(x<10){
}
2 Answers
<noob />
17,063 PointsAliayub Ali Hi, as far as i know. the difference between local and global variables are this: global variable is a variable that lives or declared not inside the function and then u can use it without declaring it later in a function and such.
local variable are variable that "lives" only in functions, for example if your
function calculate() {
int number = 5;
int antoherNumber = 7;
result = number+anotherNumber;
}
this function calculate the result from number and anotherNumber, those variables only lives inside the function if u try to use outside the function u will get an error.
if it answer ur question mark it as best answer.
Aliayub Ali
3,495 PointsWell another question forms here, what is local and global variable, if I understand that I might learn something,Thank you.
Jason Ellsworth
3,129 PointsJason Ellsworth
3,129 PointsDepends if you are using global variables or local ones for the loop in question. If you are writing a while, for, do while you have to give it some variable to act on. Sorry, your question is not very clear. If I didn't answer it can you please clear it up a bit, like, what variables are you talking about?