Scope refers to the 'visibility' of a piece of code, or what other pieces of code can access it. Let's say I want to define a variable like let pi = 3.1415926
. The scope of that variable determines whether other code can access it. If I was to write a function like this:
function getCircleArea(radius){
let pi = 3.1415926;
return radius * radius * pi;
}
The function would work correctly, and access pi to do the calculation. However, if I wrote another function that needed pi, like so:
function getCircleArea(radius){
let pi = 3.1415926;
return radius * radius * pi;
}
function getCircleVolume(radius){
return radius * radius * radius * pi *4/3;
}
The second function would not work. This is because the pi variable is only visible within the function that defines it - when the computer runs this code, it waits until getCircleArea is called, allocates some space for the pi variable, and then deletes it when the function is finished. When getCircleVolume is called, there is no pi variable for it to reference, and the function will not work. As defined right now, the pi variable has functional scope. However, if I moved the definition of pi outside of any function, like this:
let pi = 3.1415926;
function getCircleArea(radius){
return radius * radius * pi;
}
function getCircleVolume(radius){
return radius * radius * radius * pi *4/3;
}
The the pi variable would instead have global scope, meaning it can be accessed by any code in the file. Now, both functions can access pi. Apparently, use of global variables like this is frowned upon in JS, even though it's super common practise in Java. I'm sure I'll learn why soon.
There are some odd edge cases involving let
, var
, and the way they are scoped - let has block scope and var has functional scope, although there's no difference between the two in the examples above. Eloquent JS recommends using let where you can, which works fine for me, and produces less confusion.