Free · JavaScript and TypeScript · no account
Why does my code return undefined?
Paste the snippet that misbehaves. Vectra reads its structure and names the patterns that commonly produce exactly this symptom — and explains why each one matters, so you can recognise it next time.
Check a snippet
Your snippet is read as text on Vectra's server. It is not executed, it is not stored, and no model is asked about it.
What this checks
Five patterns, each one a mechanism rather than a guess. If none of them appears in your snippet, this check has nothing to say about it — which is not the same as your code being correct.
- A promise callback that returns nothing
- A `.then()` callback written with braces resolves to whatever it returns — and a body with no `return` returns `undefined`. This is the usual reason the next step in a chain receives nothing.
- A value read before it is ready
- Awaiting a response is not the same as awaiting its body. Reading a value one step too early is how a field ends up `undefined` while the request itself succeeded.
- An effect with no dependency array
- An effect with no second argument runs after every render, which is how an effect that sets state becomes an infinite loop.
- An effect that captured a value it still reads
- An empty dependency array runs once and keeps the value it saw. When that value changes, the effect still holds the old one.
- A loose comparison
- `==` converts both sides before comparing, so values of different types can compare equal or unequal in ways the code never intended.
- Text assigned as HTML
- Whatever is assigned to `innerHTML` is parsed as markup, so anything that came from a user, a URL or an API can become a `<script>` tag or an `onerror` attribute. It is the most common way a page that works in testing becomes a cross-site scripting bug.
- A type that switches checking off
- `any` is the one annotation that disables checking: every property access on it compiles, so a typo or a wrong assumption survives to runtime, which is the failure the types were added to catch.
What this is not
- It does not run your code. Nothing is executed, nothing is stored, and no model is asked about it. That means it cannot tell you what your program actually prints — only what its structure implies.
- It is not a general debugger. Five patterns is a starting set, written from the failures people most often describe. Many real `undefined` bugs are not in it.
- It reads JavaScript and TypeScript only. Paste Python and it will say so rather than guess — a pattern that is a bug in one language is correct in another.
The fastest way to see the boundary is to press Example it misses above: a snippet with a genuine bug that none of the seven rules detect.
When the pattern is not the whole problem
Finding the pattern tells you what the code does. It does not tell you which underlying concept you never got — closures, promises, scope — and that is the part that keeps costing you time across every project. Vectra is the teaching engine behind these checks: it works out which concept your confusion sits on and teaches it until you can demonstrate it.