Basics

What is hoisting — and why you can call a function before you've written it

Illustration: a theater stage — a prop is hoisted above it before the curtain opens

Here's an odd thing. The AI writes a file where a function is called on the first line but declared at the very bottom. And it works:

greet()   // "Hi!"

function greet() {
  console.log("Hi!")
}

Then you ask "make it shorter," and the AI rewrites the function as an arrow. Same code, same order. But now the page crashes:

ReferenceError: Cannot access 'greet' before initialization

One line changed — and the rules of the game changed with it. The culprit is hoisting. Let's figure out what it is, and you'll stop fearing this error.

What hoisting is

Hoisting is what JavaScript does to your code before running it. It goes through the file twice.

  1. First pass — the census. The engine finds every declaration: functions, variables. It writes the names down in its list without running anything yet.
  2. Second pass — execution. Now the code runs line by line, top to bottom.

Picture a theater. Before the curtain opens, stagehands hoist the props up in advance, so they're on hand at the right moment. The audience sees an object "appear" before anyone mentioned it. In reality, it was simply prepared ahead of time.

The whole trick is that different kinds of declarations get prepared differently.

Three kinds — three behaviors

1. function — fully ready. A regular function is hoisted together with its body. You can call it from anywhere in the file, even the first line. That's why the example above works.

2. let and const — the name is known, but hands off. The engine knows this variable will exist. But until its declaration line, it's locked. Try to read it earlier and you get that exact error, Cannot access 'x' before initialization. This no-go zone even has a name — the temporal dead zone.

An arrow function stored in a const is also a const. That's where the crash comes from: const greet = () => ... isn't hoisted whole the way function is.

3. var — the sneakiest one. The old way of declaring variables. The name is hoisted, but the value isn't — until its line, the variable equals undefined. There's no error. There's a quiet emptiness:

console.log(price)   // undefined, no error
var price = 100

That's the worst option: the program doesn't crash, it just computes with an empty value. The bug surfaces somewhere further down, and it takes ages to track.

Why this matters to you

Because AI moves code around all the time. It pushes helpers to the bottom, turns function into arrows, merges everything into one file. Every such edit can change the moment a function "exists."

Three practical takeaways:

  • See Cannot access ... before initialization? Something is used above its const or let. Check the line number in the stack trace and compare: where's the call, where's the declaration. Fix it by moving the declaration up. Or by turning the arrow into a function, if it's a helper.
  • See undefined where a value should be, and a var nearby? Suspect hoisting. Replace var with let or const: instead of a quiet emptiness you'll get an honest error. We covered the difference in let or const.
  • Don't be surprised that a function at the bottom works. It's not an AI mistake, it's a normal style: the main thing on top, details below.

Why a const further down sometimes works anyway

Now the reverse surprise. You open a React component from the AI, and a helper is declared with const at the very end of the file. And nothing crashes:

function Cart() {
  return <p>{formatPrice(100)}</p>
}

const formatPrice = (n) => "$" + n

It's all about when the code runs, not where it's written. When the file loads, Cart is only declared, not run. By the first render, the file has been read to the end, and formatPrice has long been ready.

Only what runs immediately on load crashes: a call at the top level of the file, an addEventListener with a handler declared further down. Hence a simple check: "does this line run right at load, or later — on a click, on a render?"

Where else you'll run into it

Hoisting doesn't only work on the whole file. Every function runs its own little census: names are hoisted to the top of their own scope, not of the whole file.

And classes behave like const, not like function. new User() above the class User line crashes with the same error. It's a common surprise when the AI moves a class to the end of a file.

A good prompt for these cases: "declare everything before its first use, use const and let, don't use var." Models hold this rule easily once you name it.

Does the code actually move to the top?

No, nobody rewrites the file. The engine just registers the names in advance; "hoisting" is a handy metaphor for how it looks from the outside.

Then why does everyone recommend const over function?

It's not about hoisting. const protects the name from being overwritten by accident, and an arrow behaves more predictably with this. If you need a helper at the end of the file, function is perfectly fine there.

Does Python have hoisting?

No. Python runs a file strictly top to bottom: call a function before its def and you get a NameError. But inside a function you can refer to another one declared lower, as long as you call it after the file has loaded.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →