Tiny Frontend Logo
Quizes 1130

On This Page

Quiz 1130: JavaScript Hoisting

What is hoisting in JavaScript?

Quick Answer

Hoisting in JavaScript is a behavior where variable and function declarations are "lifted" to the top of their scope before code execution. This means you can use variables and functions before they are declared in the code, but it's important to understand how hoisting works to avoid potential pitfalls.

Explain in Depth

Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their scope during the compilation phase. However, it's crucial to remember that only declarations are hoisted, not the initializations. This means you can use variables and functions before they appear in your code, but their values might not be what you expect.

Hoisting of Variables:

console.log(myVar); // Output: undefined
var myVar = 'TinyFrontend'; 

In this example, even though console.log(myVar) is executed before var myVar = 'TinyFrontend', it doesn't throw an error. This is because the declaration var myVar is hoisted to the top, but the initialization myVar = 'TinyFrontend' stays in its original place. Therefore, myVar is declared but not yet initialized, leading to undefined.

Hoisting of Functions:

greet(); // Output: Hello from TinyFrontend!
function greet() {
  console.log('Hello from TinyFrontend!');
}

Function declarations are hoisted completely, including their code. So, you can call the greet() function before it's declared in the code, and it will work as expected.

Important Considerations:

  • var declarations: var declarations are hoisted to the top of their scope.
  • let and const declarations: let and const declarations are also hoisted, but they are not initialized. Attempting to access them before their declaration will result in a ReferenceError.
  • Function expressions: Function expressions are not hoisted. Trying to use them before their declaration will throw a ReferenceError.

Example with let and const:

console.log(myLet); // Output: ReferenceError: Cannot access 'myLet' before initialization
let myLet = 'TinyFrontend';
console.log(myConst); // Output: ReferenceError: Cannot access 'myConst' before initialization
const myConst = 'tinyfrontend.com'; 

Best Practices:

  • Declare variables and functions at the top of their scope for better readability and to avoid confusion.
  • Use let or const for variable declarations in modern JavaScript code, as they provide better scoping and prevent potential hoisting issues.

Understanding hoisting is crucial for writing reliable JavaScript code. By knowing how it works, you can avoid unexpected behaviors and write code that is easier to understand and debug.

Read Next

Quiz 1156: React Portals

What are React Portals, and why are they so powerful for managing UI complexity?

What is a Prototype Chain


In JavaScript, a prototype chain is a fundamental concept that enables inheritance between objects. It's a way for an object to inherit properties and behavior from another object, allowing for code reuse and modularity.

Quiz 1154: Memoization in React

Explain the purpose of React.memo and useMemo in React, and how they contribute to performance optimization.

Quiz 1154: Memoization in React

Explain the purpose of React.memo and useMemo in React, and how they contribute to performance optimization.

Quiz 1153: CSS Containment contain

Describe the concept of CSS Containment and explain how it can enhance performance in web applications.

Quiz 1152: React State vs Props

What's the difference between state and props in React?