There are nine units in Lambda School’s Full Stack Web Development Pre-course. I’m going to summarize what the pre-course covers for each of them. This material is basically ground-floor information that students need to know before they start the full course.

Scope
Scope refers to what variables, functions and objects a part of a JavaScript program can see and has access to. There are two scopes: global scope and local scope. Local scope includes variables, functions and objects inside of a function. Global scope includes those elements that are not inside of a function. There is only one global scope, but there can be as many local scopes as there are functions in a JavaScript program.
The main purpose of scope is to limit access to resources so that they’re only available where needed. This theoretically makes bugs easier to contain and also allows the safe reuse of variable names in different functions.
As per the Lambda School text:
Scope is defined as what variables we currently have access to and where. So far in this course, we have been working in Global scope, in that we can access any variable we have created, anywhere in our code. There are a couple different levels of scope: you may have heard of block level scope (used in if statements and for loops) in which a variable using either let or const is only available within the statement or loop.
- w3schools: JavaScript Scope
- Scotch.io: Understanding Scope in JavaScript
- Mozilla Developer Network: Scope
Function Level Scope
Function level scope allows the creation of variables inside of functions that are essentially private to that function. A JavaScript program cannot reach into a function from outside and access these variables, but variables created inside of a function can be used anywhere within the given function.
Conversely, functions DO have access to variables from outside of the function, aka global variables. Functions can reach out and access variables outside of their scope, but a program cannot reach into a function to get a variable.
const var1 = ‘Lambda’
function myFunc(){
const var2 = ‘School’
console.log(var1) // Lambda
console.log(var2) // School
}
console.log(var1) // Lambda
console.log(var2) // undefined
The return Statement
Function level scope limits access to variables inside of a function – only that function (and any functions nested inside of the given function) has access to those variables. Code from outside of the given function cannot see the variables inside of it. A program might need data that was processed inside of a function to continue to operate, however. This data can be sent out from inside of a function to a global variable using the return keyword.
The way I imagine it (because I think with my stomach) is like this:
- A program sends global variables to a function like a cook sends a burger patty, bacon, cheese and a bun to a prep station (the prep station is the function and the ingredients are the variables).
- The function does something with the variables, so to continue the analogy, the cook combines the ingredients into a burger, essentially performing a function on them.
- The cook then places the burger on a tray (the processed variables have their values updated).
- Finally, the burger is taken from the tray and placed on a plate for the hungry person waiting for it (the function returns the updated values of the variables to a global variable outside of the function).
As per the Lambda School text:
We will not console.log everything that comes out of a function. Most likely we will want to return something. There is one way we can access data from within a function. In fact, it is the only way for us to get ANY data from a function, and that is to use the keyword return. Think of the return statement as the only way for data to escape a function. Nothing other than what is returned can be accessed outside of the function. Also note that when a function hits a return statement, the function immediately stops what it is doing and returns.
We can also assign the value of a return statement to another variable, and we will now have access to the data returned from the function.
function addTwoNumbers( a, b ) {
const sum = a + b;
return sum;
console.log(‘This will never be reached’);
}
const newSum = addTwoNumbers( 1, 2 );
console.log(sum) // undefined
console.log(newSum) // 3;
* Note: We will never be able to have access to the actual variable created in the function. We will only have access to the data that variable was assigned to.
2 thoughts on “Lambda School Pre-course 15: Learn to explain function scope and the return statement”