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

JavaScript

Local Scope (JavaScript)

The article im reading which was shared by Mr. McFarland from TeamTreeHouse in the teachers notes of a JS lesson says that the local scope and function scope are different but from what Ive read local scope is just another word for function scope. (I dont like reading anything that doesnt come from a reliable source such as the MDN so for me there isnt a lot of information about Local Scope at least for me.) Can anyone that learned about local scope from a reliable source explain local scope to me?

Please share the conflicting details. FYI here's the MDN Local Scope definition

Dave StSomeWhere -------- Local Scope A local scope refers to any scope defined past the global scope. There is typically one global scope, and each function defined has its own (nested) local scope. Any function defined within another function has a local scope which is linked to the outer function.

If I define a function and create variables inside it, those variables becomes locally scoped. Take this example:

// Scope A: Global scope out here var myFunction = function () { // Scope B: Local scope in here }; Any locally scoped items are not visible in the global scope - unless exposed, meaning if I define functions or variables within a new scope, it’s inaccessible outside of that current scope. A simple example of this is the following:

var myFunction = function () { var name = 'Todd'; console.log(name); // Todd }; // Uncaught ReferenceError: name is not defined console.log(name); The variable name is scoped locally, it isn’t exposed to the parent scope and therefore undefined.

-----Function scope All scopes in JavaScript are created with Function Scope only, they aren’t created by for or while loops or expression statements like if or switch. New functions = new scope - that’s the rule. A simple example to demonstrate this scope creation:

// Scope A var myFunction = function () { // Scope B var myOtherFunction = function () { // Scope C }; }; It’s easy to create new scope and create local variables/functions/objects.

2 Answers

Hello

local scope is more of a general term used as it means the scope is local to whatever that case. the following are local scopes:

Module scope

//console.js
const localExample = 'local example works';
module.exports.moduleExample = 'module example works';


//app.js
const moduleTest = require('./console');

console.log(`local variable ${moduleTest.localExample}`);
console.log(`module variable ${moduleTest.moduleExample}`);

//output

local variable undefined
module variable module example works

This is due to the localExample only being local to that module and therefore in the local scope.

Function scope

//app.js
const globalScope = 'global scope example works';

function functionExample() {
    const localExample = 'local scope example works';
}
console.log(`The ${globalScope}`);
console.log(`The ${localExample}`);

//output

The global scope example works
ReferenceError: localExample is not defined

The localExample is only set within the brackets of the function and therefore making it local scope.

Block scope

Block scope is pretty much the same as function scope but is local to any block, that mean objects literals, functions, conditionals, etc

Something worth mentioning, let and const are both block scoped, unlike var so they should be declared at the top of each block.

This will not work:

(function () {

    console.log(blockTest);
    const blockTest = 'The is a block scope test.';
})();

Check this trehouse workshop for more on let and const

If you want to find out more about scope in-depth, I would recommend the YDKJS series from Kyle Simpson.

It is available for free online on GitHub.

https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch1.md

TL;DR section

https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch1.md#review-tldr