What is Hoisting?
About 1 min
What is Hoisting? 관련
The JavaScript Interview Prep Handbook – Essential Topics to Know + Code Examples
JavaScript is a widely used language in web development and powers interactive features of virtually every website out there. JavaScript makes it possible to create dynamic web pages and is very versatile. JavaScript remains one of the most in-demand programming languages in 2024. Many companies are looking for proficiency in...
The JavaScript Interview Prep Handbook – Essential Topics to Know + Code Examples
JavaScript is a widely used language in web development and powers interactive features of virtually every website out there. JavaScript makes it possible to create dynamic web pages and is very versatile. JavaScript remains one of the most in-demand programming languages in 2024. Many companies are looking for proficiency in...
Hoisting refers to JavaScript's default behavior that moves all variables and function declarations to the top. This means you can use them before they are declared.
x=5
console.log(x) // prints 5
var x
In the code above, JavaScript has moved the variable declaration to the top of the code block. That is: it is similar to declaring x
at the first line.
In the case of functions, the declarations are also moved to top:
function foo() {
console.log("foo called");
}
foo(); // foo called
However, this does not work with let
and const
.
x = 5; // throws ReferenceError
let x;
fiz(); // throws ReferenceError
const fiz = () => { console.log("fiz called") };