
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
In this lesson, you'll learn about in JavaScript, a mechanism that affects how variables and functions are declared and accessed in your code.
is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. This means that no matter where functions and variables are declared, they are moved to the top of their scope, whether global or local.
When JavaScript compiles your code, all variable declarations using are hoisted to the top of their functional (if declared inside a function) or global scope (if declared outside a function), regardless of where the actual declaration is made. This is what we mean by "hoisting."
In JavaScript, an undeclared variable is assigned the value at execution and is also of type .
console.log(typeof name); // undefined
A is thrown when trying to access a previously undeclared variable .
console.log(name); // ReferenceError: name is not defined
Key thing to note regarding hoisting is that only the variable declaration is moved to the top, not the actual value assigned to the variable.
console.log(myString); // undefined
var myString = 'test';
This is equivalent to:
var myString;
console.log(myString); // undefined
myString = 'test';
Example 1:
var hoist;
console.log(hoist); // undefined
hoist = 'The variable has been hoisted.';
Example 2:
function hoist() {
var message;
console.log(message);
message = 'Hoisting is cool!';
}
hoist(); // undefined
JavaScript only hoists declarations, not initializations. If a variable is declared and initialized after using it, the value will be .
console.log(num); // undefined
var num;
num = 6;
To avoid this pitfall, always declare and initialize the variable before using it:
function hoist() {
var message = 'Hoisting is cool!';
return message;
}
hoist(); // Hoisting is cool!
The variable declaration, , whose scope is the function , is hoisted to the top of the function.
and are also hoisted, but you cannot access them before the actual declaration is evaluated at runtime. What does this mean? Let's see it in a simple example:
console.log(myVarString); // undefined
var myVarString = 'var';
console.log(myLetString); // ReferenceError
let myLetString = 'let';
console.log(myConstString); // ReferenceError
const myConstString = 'const';
With and , you get exactly what you would expect: a . This is JavaScript's way of encouraging clean code.
You should always declare variables before using them.
Like variables, function declarations are completely hoisted to the top.
hoisted(); // 'This function has been hoisted.'
function hoisted() {
console.log('This function has been hoisted.');
}
Function expressions, the more modern way of writing functions with the keyword, are not hoisted.
functionExpression(); // ReferenceError
const functionExpression = () => {
console.log('Will this work?');
}
, like (which we'll discuss in the next lesson), are complex topics. They might not be useful in everyday coding, but understanding them is important for technical interviews. If you stick to good programming habits, you won't encounter hoisting or closures often. However, knowing about these "tricky concepts" can be beneficial, especially in interview scenarios.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
How did you manage to remove the blur property and reach here?
Upgrading gives you access to quizzes so you can test your knowledge, track progress, and improve your skills.
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
##Looks like we found a thief monkey By the way, I liked the trick how you reached till here. You have a good sense of humor. You will improve a lot if you join our course with this passion.
var
(function-scoped, outdated)let
(block-scoped, modern and recommended)const
(block-scoped, cannot be reassigned)_
, or $
let let = 5;
is invalid)myVar
and myvar
are different)string
, number
, boolean
, null
, undefined
, bigint
, symbol
Objects
, Arrays
, Functions
Subscribing gives you access to a brief, insightful summary of each lecture to stay on track.
00:00:00 Let's talk about hoisting, a JavaScript mechanism that affects how variables and functions are declared and accessed within your code.
00:00:09 Sounds similar to scope, doesn't it?
00:00:11 But what hoisting does is it moves the variables to the top of their scope before the code gets executed.
00:00:19 This means that no matter where functions and variables are declared, they are moved to the top of their scope, whether global or local.
00:00:28 To start talking about the concept of hoisting, you have to understand how variables get created in the first place.
00:00:34 And you already know that, right?
00:00:36 But just to recap, if you try to console log a variable that doesn't yet exist, like the first name, you'll see you'll get an error.
00:00:47 And the type of that variable, which doesn't yet exist is what?
00:00:51 Try to answer in three, two, one.
00:00:54 Well, it is undefined because it hasn't yet been declared before.
00:00:58 So what if you declare the variable, let's call it myString and make it equal to a string that says test.
00:01:06 And if you try to console log it right here, myString, we already know that.
00:01:11 It is within the scope and we can call it.
00:01:13 But have you ever asked yourself what would happen if you tried console logging the variable before it gets declared?
00:01:21 Of course, the answer is that you cannot access it.
00:01:25 And that means that hoisting doesn't work on const and let declared variables, because it's not moving this variable declaration to the top of the scope.
00:01:36 But if you declare it using the var keyword, as you can notice, now we're not getting a reference error, but we're just getting undefined.
00:01:44 So it moved the actual variable declaration at the top, like this, var myString, but it did not move the actual value that we used to declare that variable with.
00:01:55 But as I said, this only works with the var and doesn't do anything if you try to do it with const or let, which is the only thing you should be using nowadays.
00:02:05 So why is that?
00:02:07 Well, the concept of hoisting is just another reason of why we're not using the var keyword anymore in the newer versions of JavaScript.
00:02:14 JavaScript is moving away of this inconsistent style where you don't know what's where.
00:02:19 See, it's good to know that hoisting exists, but you should never rely on it.
00:02:24 It's always better to follow best programming practices and declare variables exactly where they should be, at the top of the scope from within which they're
00:02:35 being used.
00:02:36 This ensures your code is predictable and doesn't rely on hosting.
00:02:41 With let and const, you get exactly what you would expect.
00:02:44 If you're trying to use a variable that hasn't yet been declared, you get an error.
00:02:49 If you declared before, it works.
00:02:51 basically making you write better code.
00:02:54 It's common sense.
00:02:55 And common sense is exactly what we want in your applications.
00:02:59 See, hoisting isn't that difficult.
00:03:01 And you will never use it in everyday coding.
00:03:03 If you stick to good programming habits, you won't even know it exists.
00:03:07 But as I said, understanding these concepts can be useful in some crazy JavaScript interviews.
00:03:12 And last, let's talk about the concept of closures.