In plain words
Scope is the part of the code where a variable exists, so a name created inside a function or block can't be seen outside it.
Where you see it
Curly braces mark the edges. When the agent creates discount inside an if block and uses it further down, you get "discount is not defined", even though you can see it a few lines up.
What goes wrong
- A name declared one block too deep
const and let only live inside the nearest pair of braces. Declare the variable above the if, so the lines below can reach it.
- Two variables with the same name
A user inside a function hides a different user outside it. The code reads the inner one, and you spend an hour wondering why it's the wrong person.
- Keeping the current user at the top of a server file
A variable outside any function outlives the request, so visitors share it. One person's data can turn up in someone else's response.
let currentUser; // top of app/api/orders/route.ts
What to tell your agent
Quick check
Sign in to earn +10 XP for itAn API route stores the logged-in user in a variable at the top of the file. What can happen?