JavaScript Scope

Akiko Green
2 min readJul 22, 2020
Halo Top Ice Cream Image

I remember encountering a plethora of errors regarding the Javascript scope. An error you will be more than acquainted with is the : RefrenceError. This just means when you assign a value to a variable without the let, or const keywords or if a variable/function is not in your current scope, JavaScript will yell at you and throw a RefrenceError the message that looks similar to this:

RefrenceError: assignment to undeclared variable "x"

Global Scope

Global scope is when a variable is declared outside of all functions, giving full access anywhere in your code!

let myGlobalVariable = "I am global, anyone can touch me"function whereAmI(){
console.log(myGlobalVariable)
}
console.log(myGlobalVariable) // "I am global, anyone can touch me"
whereAmI() // "I am global, anyone can touch me"

Although it may be tempting to declare variables in the global scope, it is highly advised not to in case of naming collisions! Having two variables named the same thing will cause a collision and will raise an error as well.

--

--

Akiko Green

Software Engineer, Novice Blogger, Indoor Climbing Cutie 🥰