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 trialAli Dahud
3,459 PointsCan you explain to me in more depth?
what is a variable scope? because in the video it's explained poorly Steven Parker
1 Answer
Steven Parker
231,261 PointsSure, I'll give it a shot, but for future questions try giving the whole community some time to answer before tagging anyone specifically. There are lots of folks wiling to help.
So "variable scope" is basically a way to describe what part of the program a variable can be accessed in. The most common cases are "global scope", which means the variable can be accessed anywhere, "function scope" where the variable can be accessed only within a specific function, and "block scope" where the variable can be accessed only inside a code block (between a pair of braces {}). Here's some examples:
var globby = 2; // global scope
function test(x) {
if (x == 1) {
var funky = 3; // function scope
let blocky = 4; // block scope
console.log(typeof globby); // "number"
console.log(typeof funky); // "number"
console.log(typeof blocky); // "number"
}
console.log(typeof globby); // "number"
console.log(typeof funky); // "number" (still in scope)
console.log(typeof blocky); // "undefined"
}
test(1);
console.log(typeof globby); // "number"
console.log(typeof funky); // "undefined"
console.log(typeof blocky); // "undefined"
Ali Dahud
3,459 PointsAli Dahud
3,459 PointsThank you but youβre so responsive and youβre explaining it extremely well.
Ali Dahud
3,459 PointsAli Dahud
3,459 PointsAnd if we canβt be in direct contact then why wouldnβt I?
Steven Parker
231,261 PointsSteven Parker
231,261 PointsThat's very nice, and I'm flattered. But I'm sure input from various sources would be beneficial to your learning. Give the community as a whole a chance.
Plus I do answer questions where I'm not tagged.