Chapter 17 · Reference
Finding and fixing mistakes
Everyone's code goes wrong, every day, including experts'. What matters is finding out what went wrong and where. In this chapter you'll learn to read the browser's console, understand Nyx's messages and JavaScript's own errors, and fix problems quickly.
Opening the console
Browsers have a hidden panel for developers called the console. It's where error messages appear, both from JavaScript and from Nyx. A visitor never sees it, so a page can be broken without anything showing on the screen. Get used to keeping it open while you work.
Messages come in three colours:
Your own console.log(value) lines appear here too, which is the quickest way to check what a variable holds. On this website, print() does the same job inside the examples, as you learned in Getting started.
Reading a Nyx message
When Nyx spots a mistake, it writes a message in the console that answers three questions: what is wrong, where it happened, and how to fix it.
Nyx: Nothing matches "#sumbit". (app.js, line 4)
-> Did you mean "#submit"?
Nyx:This message comes from Nyx, not from JavaScript itself.Nothing matches "#sumbit".What went wrong, in plain English.(app.js, line 4)Where: the file and line in your own code. Open that file and go to that line.-> Did you mean "#submit"?How to fix it. Often it spots the exact typo.The file and line appear when your code is in its own .js file, or in a <script> on a page opened through a server like Live Server. Examples on this website don't have a file of their own, so their messages appear without a line.
Making mistakes on purpose
The best way to learn the messages is to cause them. Each line below has one mistake. Press Run and read what Nyx says about each one:
A card
Fix the lines one at a time and press Run again, until the Output is empty.
JavaScript's own errors
Some mistakes break the JavaScript language itself, not Nyx: a misspelled variable, a missing bracket. JavaScript reports these in red, in short, technical English. Nyx adds a "That error means..." note under the most common ones, with the file and line.
Score: 0
Click +1: the Output shows JavaScript's error and Nyx's explanation. Find the typo and fix it.
Here's every JavaScript error Nyx explains, and how to fix each one:
scroe is not definedThat name doesn't exist. Check the spelling (capitals count), create it with const or let first, or put quotes around text.Cannot read properties of undefined (reading 'text')You used .text on something empty. Usually a typo in a name, a search that found nothing, or data that hasn't arrived (a missing await).nyx.tost is not a functionThat isn't a function, so you can't call it with ( ). Check the spelling of the method.await is only valid in async functionsAdd async to the function around the await.Assignment to constant variableYou changed a const. Use let for values that change.Cannot access 'score' before initializationYou used a name before the line that creates it. Move that const or let line up.... is not iterableYou looped over something that isn't a list. Check it's an array, and that server data was awaited.Unexpected end of inputA {, ( or [ is never closed. Count your brackets.Unexpected token, missing ) after argument listJavaScript couldn't read the code: a missing or extra comma, quote or bracket near that line.Firefox and Safari word some errors a little differently, like box is undefined. Nyx recognises those too.
Explaining errors on the page: nyx.explain
nyx.explain(error) gives you Nyx's plain-English version of an error, as { message, hint }, or null if it doesn't recognise it. Use it in a catch to show something friendlier than the technical text. Type name or city, then something that isn't there, like nmae:
student[field]Reads the field whose name is infield. With"city"it's the same asstudent.city. A name that isn't there givesundefined..toUpperCase()Turns text into capital letters. Onundefined, JavaScript throws "Cannot read properties of undefined".nyx.explain(error)Turns that into "You used .toUpperCase on a value that is empty (undefined)."
Errors from Nyx's own requests (from Servers and AJAX) work too: you get their message and hint.
When nothing happens, and nothing is said
The hardest problems are silent: you click, nothing happens, and the console is empty. Go through this list from the top:
404 means a wrong file name or folder. Or write console.log("loaded") at the top of your file and look for it.<script> must come first, as in Getting started.nyx(() => { ... }), or put your script at the end of the page.console.log("clicked") inside it. No message means the click never reached it.nyx.quietMode = true left in your code.Good debugging habits
- Read the first red message firstOne mistake often causes several messages after it. Fixing the first can make the rest disappear.
- Go to the line it namesThen look at the line just above too: a missing bracket is often noticed one line late.
- Check what values really are
console.log(value)(orprint()on this website) shows what a variable holds right now. Guessing is slower. - Change one thing at a timeThen test. If you change five things at once and it works, you won't know which one fixed it.
- Make the problem smallDelete or comment out code until the mistake is obvious, then add it back.
- Explain your code out loudLine by line, to a friend, or to a rubber duck. You'll often hear the mistake as you say it.
Showing messages on the page: nyx.listen
nyx.listen() runs your function for every message Nyx gives, even when the console is closed. It's handy for a debug panel on a phone, where the console is hard to reach. This is how the Output in this website's examples shows Nyx's messages.
m.level"error", "warn" or "tip".m.messageWhat went wrong.m.hintHow to fix it, or empty text.m.where{ file, line } in your code, or null when it isn't known.m.timeWhen it happened, as a Date.Like the timers, nyx.listen() gives back something with .stop(): listener.stop() stops listening.
Settings
nyx.help()Prints a cheat sheet of Nyx's most useful tools in the console.nyx.explainMissing = falseStops the "Nothing matches" messages, for pages that check on purpose whether an element exists.nyx.explainErrors = falseStops the "That error means..." notes under JavaScript's own errors.nyx.quietMode = trueStops Nyx writing in the console at all. nyx.listen() still gets every message.Leave all of these as they are while you're learning. The messages only appear in the console, so visitors never see them.
Every Nyx message
Here's every message Nyx can give, grouped by chapter, with what to do. Messages are shortened: the real ones name your exact selector, field or value.
Finding elements
.card instead of #card, or the other way round.# for an id or . for a class.nyx(() => { ... }).nyx() a selector in quotes, an element or a list of elements.Changing content, styles and events
.value("") clears it..fadeIn() or .slideDown(), and .done() to run code after.() => { ... }..picker("li").Animation
Servers and AJAX
nyx.get("/api/posts").nyx.get() instead of nyx.post(), or the other way round.nyx.debounce().{ timeout: "60 seconds" }..load().WordPress
add_action( 'wp_ajax_like', ... ), plus wp_ajax_nopriv_like for visitors.WP_DEBUG.wp_send_json_success() instead of echo.wp_send_json_error() message.nyx.wp.ajax("like_post", data).Timers
"5 minutes".nyx.after("2 seconds", fn).() => { ... }."5 seconds"."2027-02-15T10:00".Formatting
Number() and check it isn't empty."INR".{ decimals: 0 }."2026-11-29".nyx.plural(3, "student").Saving data and smart state
nyx.remember("theme", "large").nyx.state({ count: 0 }).{ remember: "name" }.Checking forms
name, spelled exactly."min 8".email: "required, email".<form>, not a <div>.nyx.rule("no spaces", (value) => ...).Searching lists
nyx("#list li").search(text).Everyday helpers
nyx.copy(nyx("#code").text()).Number(...) for text from inputs.{ Maths: 92, Science: 85 }.Exercise
Fill in the blanks, then press Check answers.
1. Show a value in the console while you work.
console.(score);
2. JavaScript says scroe is not defined. Which word did you probably mean?
3. await is only valid in async functions. Add the missing word.
nyx("#load").on("click", () => { await nyx.get(url); });
4. Assignment to constant variable. Fix the first word.
count = 0; count++;
5. Get Nyx's plain-English version of an error.
const help = nyx.(error);
6. Run a function for every Nyx message.
nyx.((m) => showOnPage(m.message));
7. Print Nyx's cheat sheet in the console.
nyx.();
Remember
console.log() inside listeners.nyx.explain() translates an error; nyx.listen() shows every message on the page.