Variables, Scope and Hoisting in JavaScript
Introduction
We must understand how variable scope and variable hoisting work in JavaScript, if want to understand JavaScript well.
Variables are one of the fundamental blocks of any programming language, the way each language defines how we declare and interact with variables can make or break a programming language. Thus any developer needs to understand how to effectively work with variables, their rules, and particularities.
So this makes Variables a fundamental programming concept, and one of the first and most important things to learn. In JavaScript, there are three ways to declare a variable - with the keywords var, let, and const. Each with its own properties and particularities.
Keyword | Scope | Hoisting | Can be reassigned |
---|---|---|---|
var | Function | Yes | yes |
let | Block | No | Yes |
const | Block | No | No |
In this article, we will learn what variables are, how to declare and name them, the difference between var, let, and const, and the significance of global and local scope.
What are Variables?
A variable is a named container used for storing values. A piece of information that we might reference multiple times can be stored in a variable for later use or modification.
Variables in algebra, frequently represented by x, are used to hold the value of an unknown number. In JavaScript, the value contained inside a variable can be more than just a number; it can be any JavaScript data type, such as a string or an object.
// Assign the string value Friendly to the username identifier
var username = 'friendly_dolphin'
This statement consists of a few parts:
The declaration of a variable using the var keyword The variable name (or identifier), username The assignment operation, represented by the = syntax The value being assigned, "friendlydolphin" Now we can use username in code, and JavaScript will remember that username represents the string value friendlydolphin.
// Check if variable is equal to value
if (username === 'friendly_dolphin') {
console.log(true)
}
Variable Scope
Scope in JavaScript refers to context (or portion) of the code which determines the accessibility (visibility) of variables. In JavaScript, we have 2 types of scope, local and global. Though local scope can have different meanings.
Let’s work out through the definitions by giving some examples of how scoping works. Let’s say you define a variable message:
const message = 'Hello World'
console.log(message) // 'Hello World'
As you may expect the variable message used in the console.log would exist and have the value Hello World
. No Doubts there, but what happens if I change a bit where I declare the variable:
if (true) {
const message = 'Hello World'
}
console.log(message) // ReferenceError: message is not defined
Looks like broken, but why? Actually if statement creates a local block scope, and since I used const the variable is only declared for that block scope, and cannot be accessed from the outside.
Let’s dig a bit more about block and function scopes.
Block Scope
A block is basically a section of code (zero or more statements) which is delimited by a pair of curly braces and may optionally be labeled.
As we already discussed the use of let and const allows us to define variables that live within the block scope. Next, we’ll build very similar examples by using different keywords to generate new scopes:
const x1 = 1
{
const x1 = 2
console.log(x1) // 2
}
console.log(x1) // 1
Let’s explain this one as it may look a bit strange at first. In our outer scope, I am defining the variable x1
with a value of 1
. Then create a new block scope by simply using curly braces, this is strange, but totally legal within JavaScript, and in this new scope, I create a new variable (separate from the one in the outer scope) also named x1
. But don’t get confused, this is a brand new variable, which will only be available within that scope.
Same example now with a named scope:
const x2 = 1
myNewScope: { // Named scope
const x2 = 2
console.log(x2) // 2
}
console.log(x2) // 1
While example (DO NOT run the code below? 🍄 🤔)
const x3 = 1
while (x3 === 1) {
const x3 = 2
console.log(x3) // 2
}
console.log(x3) // Never executed
What’s wrong with that code? And what would happen if it is run?
👉 Let me explain, x3
as declared in the outer scope is used for the while comparison x3 === 1
, normally inside the while statement, I’d be able to reassign x3
a new value and exit the loop, however as I am declaring a new x3
withing the block scope, I cannot change x3
from the outer scope anymore, and thus the while condition will always evaluate to true producing an infinite loop that will hang THE browser, or if you are using a terminal to run it on NodeJS will print a lot of 2
.
Fixing this particular code could be tricky unless you actually rename either variables.
So far in THE example, IS used const, but exactly the same behavior would happen with let. However, as we can see in the comparison table above that the keyword var is actually function scope, so what does it mean for the examples? Well… let’s take a look:
var x4 = 1
{
var x4 = 2
console.log(x4) // 2
}
console.log(x4) // 2
🤔 even though re-declared x4
inside the scope it changed the value to 2
on the inner scope as well as the outer scope. An this is one of the most important differences between let, const, and var .
Function Scope
A function scope is in a way also a block scope, so let and const would behave the same way they did in our previous examples. However, function scopes also encapsulate variables declared with var. but let’s see that continuing with our xn examples:
🤔 const or let example:
const x5 = 1
function myFunction() {
const x5 = 2
console.log(x5) // 2
}
myFunction()
console.log(x5) // 1
👉as expected, now with var
var x6 = 1
function myFunction() {
var x6 = 2
console.log(x6) // 2
}
myFunction()
console.log(x6) // 1
👉In this scenario, var worked the same way as let and const. Moreover:
function myFunction() {
var x7 = 1
}
console.log(x7) // ReferenceError: x7 is not defined
❗ ⚠️ Clearly, var declarations only exist within the function they were created in and can’t be accessed from the outside.
But there’s more to it, as always JS has been evolving, and newer type of scopes has been created.
Module Scope
With the introduction of modules in ES6, it was important for variables in a module not to directly affect variables in other modules. Can you imagine a world where importing modules from a library would conflict with your variables? Not even JS is that messy! So by definition modules create their own scope which encapsulates all variables created with var, let or const, similar to the function scope.
There are ways though that modules provide to export variables so they can be accessed from outside the module.
So far we talked about different types of local scopes, let’s now dive into global scopes.
Global Scope
A variable defined outside any function, block, or module scope has global scope. Variables in global scope can be accessed from everywhere in the application.
The global scope can sometimes be confused with module scope, but this is not the case, a global scope variable can be used across modules, though this is considered a bad practice, and for good reasons.
How would you go about declaring a global variable? It depends on the context, it is different on a browser than a NodeJS application. In the context of the browser, you can do something as simply as:
<script>let MESSAGE = 'Hello World' console.log(MESSAGE)</script>
Or by using the window object:
<script>window.MESSAGE = 'Hello World' console.log(MESSAGE)</script>
There are some reasons you wanna do something like this, however, always be careful when you do it.
Nesting scopes
it is possible to nest scopes, meaning to create a scope within another scope, and its a very common practice. Simply by adding an if statement inside a function we are doing this. So let’s see an example:
function nestedScopes() {
const message = 'Hello World!'
if (true) {
const fromIf = 'Hello If Purple!'
console.log(message) // Hello World!
}
console.log(fromIf) // ReferenceError: fromIf is not defined
}
nestedScopes()
Lexical Scope
In a way, we already made use of lexical scope, though we didn’t know about it. Lexical scope simply means that the children scopes have access to the variables defined in outer scopes.
function outerScope() {
var name = 'Irene'
function innerScope() {
console.log(name) // 'Irene'
}
return innerScope
}
const inner = outerScope()
inner()
That looks stranger than what it is, so let’s explain it. The function outerScope declares a variable name with value Juan and a function named innerScope. The later does not declare any variables for its own scope but makes use of the variable name declared in the outer function scope.
When outerScope() gets called it returns a reference to the innerScope function, which is later called from the outermost scope. When reading this code for the first time you may be confused as to why innerScope would console.log the value Juan as we are calling it from the global scope, or module scope, where name is not declared. The reason why this works is thanks to JavaScript closures.
Hoisting
Hoisting in terms of JavaScript means that a variable is created in memory during the compile phase, and thus they can actually be used before they are actually declared. Sounds super confusing, let’s better see it in code.
function displayName(name) {
console.log(name)
}
displayName('Irene')
//***********************
// Outputs
//***********************
// 'Irene'
but what would you think of the following:
hoistedDisplayName('Irene')
function hoistedDisplayName(name) {
console.log(name)
}
//***********************
// Outputs
//***********************
// 'Irene'
👉 since the function is assigned to memory before the code actually runs, the function hoistedDisplayName is available before its actual definition, at least in terms of code lines.
👉 Functions have this particular property, but also do variables declared with var. Let’s see an example:
console.log(x8) // undefined
var x8 = 'Hello World!'
The fact that the variable is “created” before its actual definition in the code doesn’t mean that its value is already assigned, this is why when we do the console.log(x8) we don’t get an error saying that the variable is not declared, but rather the variable has value undefined. Very interesting, but what happens if we use let or const? Remember in our table they don’t share this property.
console.log(x9) // Cannot access 'x9' before initialization
const x9 = 'Hello World!'
It threw an error.
Hoisting is a lesser-known property of JavaScript variables, but it’s also an important one. Make sure you understand the differences, it is important for your code, and it may be a topic for an interview question.
Reassignment of variables
This topic covers specifically variables declared with the keyword const. A variable declared with const cannot be reassigned, meaning that we can’t change its value for a new one, but there’s a trick. Let’s see some examples:
const c1 = 'hello world!'
c1 = 'Hello World' // TypeError: Assignment to constant variable.
As expected, we can’t change the value of a constant, or can we?
const c2 = { name: 'Irene' }
console.log(c2.name) // 'Irene'
c2.name = 'Daniel'
console.log(c2.name) // 'Daniel'
Did we just change the value of a const value? The short answer is NO. Our constant c2 references an object with a property name. c2 is a reference to that object, that’s its value. When we do c2.name we are really taking the pointer to the c2 object and accessing the property from there. What we are changing when we do c2.name is the value of the property name in the object, but not the reference stored in c2, and thus c2 remained constant though the property value is now different. what happens when we actually try to update the value differently:
const c3 = { name: 'Irene' }
console.log(c3.name) // 'Irene'
c3 = { name: 'Daniel' } // TypeError: Assignment to constant variable.
console.log(c3.name)
Even though the object looks the same, we are actually creating a new object { name: 'Daniel' } and trying to assign that new object to c3, but we can’t as it was declared as constant.