1-4 What Went Wrong - Troubleshooting JavaScript - Learn Web Development - MDN
1-4 What Went Wrong - Troubleshooting JavaScript - Learn Web Development - MDN
Types of error
Generally speaking, when you do something wrong in code, there are two main types of
error that you'll come across:
Syntax errors: These are spelling errors in your code that actually cause the program
not to run at all, or stop working part way through — you will usually be provided with
some error messages too. These are usually okay to fix, as long as you are familiar
with the right tools and know what the error messages mean!
Logic errors: These are errors where the syntax is actually correct but the code is not
what you intended it to be, meaning that program runs successfully but gives
incorrect results. These are often harder to fix than syntax errors, as there usually isn't
an error message to direct you to the source of the error.
Okay, so it's not quite that simple — there are some other differentiators as you drill down
deeper. But the above classifications will do at this early stage in your career. We'll look at
both of these types going forward.
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong 1/12
31/05/2023, 15:34 What went wrong? Troubleshooting JavaScript - Learn web development | MDN
An erroneous example
To get started, let's return to our number guessing game — except this time we'll be
exploring a version that has some deliberate errors introduced. Go to GitHub and make
yourself a local copy of number-game-errors.html (see it running live here ).
1. To get started, open the local copy inside your favorite text editor, and your browser.
2. Try playing the game — you'll notice that when you press the "Submit guess" button,
it doesn't work!
Note: You might well have your own version of the game example that doesn't
work, which you might want to fix! We'd still like you to work through the article
with our version, so that you can learn the techniques we are teaching here. Then
you can go back and try to fix your example.
At this point, let's consult the developer console to see if it reports any syntax errors, then
try to fix them. You'll learn how below.
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong 2/12
31/05/2023, 15:34 What went wrong? Troubleshooting JavaScript - Learn web development | MDN
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong 3/12
31/05/2023, 15:34 What went wrong? Troubleshooting JavaScript - Learn web development | MDN
Note: See our TypeError: "x" is not a function reference page for more details
about this error.
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong 4/12
31/05/2023, 15:34 What went wrong? Troubleshooting JavaScript - Learn web development | MDN
Depending on the browser you are using, you might see a different message here. The
message above is what Firefox will show you, but Chrome, for example, will show you
this:
Uncaught TypeError: Cannot set properties of null (setting 'textContent')
It's the same error, but different browsers describe it in a different way.
Note: This error didn't come up as soon as the page was loaded because
this error occurred inside a function (inside the checkGuess() { } block). As
you'll learn in more detail in our later functions article, code inside functions
runs in a separate scope than code outside functions. In this case, the code
was not run and the error was not thrown until the checkGuess() function was
run by line 86.
4. The line number given in the error is 80. Have a look at line 80, and you'll see the
following code:
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong 5/12
31/05/2023, 15:34 What went wrong? Troubleshooting JavaScript - Learn web development | MDN
5. This line is trying to set the textContent property of the lowOrHi variable to a text
string, but it's not working because lowOrHi does not contain what it's supposed to.
Let's see why this is — try searching for other instances of lowOrHi in the code. The
earliest instance you'll find is on line 49:
const lowOrHi = document.querySelector("lowOrHi");
6. At this point we are trying to make the variable contain a reference to an element in
the document's HTML. Let's see what the variable contains after this line has been
run. Add the following code on line 50:
console.log(lowOrHi);
This code will print the value of lowOrHi to the console after we tried to set it in line
49. See console.log() for more information.
7. Save and refresh, and you should now see the console.log() result in your console.
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong 6/12
31/05/2023, 15:34 What went wrong? Troubleshooting JavaScript - Learn web development | MDN
Sure enough, lowOrHi 's value is null at this point, and this matches up with the
Firefox error message lowOrHi is null . So there is definitely a problem with line 49.
The null value means "nothing", or "no value". So our code to set lowOrHi to an
element is going wrong.
8. Let's think about what the problem could be. Line 49 is using a
document.querySelector() method to get a reference to an element by selecting it with
a CSS selector. Looking further up our file, we can find the paragraph in question:
<p class="lowOrHi"></p>
9. So we need a class selector here, which begins with a dot ( . ), but the selector being
passed into the querySelector() method in line 49 has no dot. This could be the
problem! Try changing lowOrHi to .lowOrHi in line 49.
10. Try saving and refreshing again, and your console.log() statement should return the
<p> element we want. Phew! Another error fixed! You can delete your console.log()
Note: See our TypeError: "x" is (not) "y" reference page for more details about
this error.
A logic error
At this point, the game should play through fine, however after playing through a few
times you'll undoubtedly notice that the game always chooses 1 as the "random" number
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong 7/12
31/05/2023, 15:34 What went wrong? Troubleshooting JavaScript - Learn web development | MDN
you've got to guess. Definitely not quite how we want the game to play out!
There's definitely a problem in the game logic somewhere — the game is not returning an
error; it just isn't playing right.
1. Search for the randomNumber variable, and the lines where the random number is first
set. The instance that stores the random number that we want to guess at the start of
the game should be around line number 45:
let randomNumber = Math.floor(Math.random()) + 1;
2. And the one that generates the random number before each subsequent game is
around line 113:
randomNumber = Math.floor(Math.random()) + 1;
3. To check whether these lines are indeed the problem, let's turn to our friend
console.log() again — insert the following line directly below each of the above two
lines:
console.log(randomNumber);
4. Save and refresh, then play a few games — you'll see that randomNumber is equal to 1 at
each point where it is logged to the console.
Working through the logic
To fix this, let's consider how this line is working. First, we invoke Math.random() , which
generates a random decimal number between 0 and 1, e.g. 0.5675493843.
Math.random();
Next, we pass the result of invoking Math.random() through Math.floor() , which rounds the
number passed to it down to the nearest whole number. We then add 1 to that result:
Math.floor(Math.random()) + 1;
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong 8/12
31/05/2023, 15:34 What went wrong? Troubleshooting JavaScript - Learn web development | MDN
Rounding a random decimal number between 0 and 1 down will always return 0, so adding
1 to it will always return 1. We need to multiply the random number by 100 before we round
it down. The following would give us a random number between 0 and 99:
Math.floor(Math.random() * 100);
Try updating both lines like this, then save and refresh — the game should now play like
we are intending it to!
to
It throws this error because it thinks you are trying to do something different. You should
make sure that you don't mix up the assignment operator ( = ) — which sets a variable to
be equal to a value — with the strict equality operator ( === ), which tests whether one
value is equal to another, and returns a true / false result.
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong 9/12
31/05/2023, 15:34 What went wrong? Troubleshooting JavaScript - Learn web development | MDN
Note: See our SyntaxError: missing ; before statement reference page for more
details about this error.
The program always says you've won, regardless of the guess you
enter
This could be another symptom of mixing up the assignment and strict equality operators.
For example, if we were to change this line inside checkGuess() :
if (userGuess === randomNumber) {
to
if (userGuess = randomNumber) {
the test would always return true , causing the program to report that the game has been
won. Be careful!
SyntaxError: missing ) after argument list
This one is pretty simple — it generally means that you've missed the closing parenthesis
at the end of a function/method call.
Note: See our SyntaxError: missing ) after argument list reference page for more
details about this error.
to
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong 10/12
31/05/2023, 15:34 What went wrong? Troubleshooting JavaScript - Learn web development | MDN
function checkGuess( {
This has caused the browser to think that we are trying to pass the contents of the
function into the function as an argument. Be careful with those parentheses!
SyntaxError: missing } after function body
This is easy — it generally means that you've missed one of your curly braces from a
function or conditional structure. We got this error by deleting one of the closing curly
braces near the bottom of the checkGuess() function.
SyntaxError: expected expression, got 'string' or SyntaxError:
unterminated string literal
These errors generally mean that you've left off a string value's opening or closing quote
mark. In the first error above, string would be replaced with the unexpected character(s)
that the browser found instead of a quote mark at the start of a string. The second error
means that the string has not been ended with a quote mark.
For all of these errors, think about how we tackled the examples we looked at in the
walkthrough. When an error arises, look at the line number you are given, go to that line
and see if you can spot what's wrong. Bear in mind that the error is not necessarily going
to be on that line, and also that the error might not be caused by the exact same problem
we cited above!
Summary
So there we have it, the basics of figuring out errors in simple JavaScript programs. It
won't always be that simple to work out what's wrong in your code, but at least this will
save you a few hours of sleep and allow you to progress a bit faster when things don't turn
out right, especially in the earlier stages of your learning journey.
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong 11/12
31/05/2023, 15:34 What went wrong? Troubleshooting JavaScript - Learn web development | MDN
See also
There are many other types of errors that aren't listed here; we are compiling a
reference that explains what they mean in detail — see the JavaScript error reference.
If you come across any errors in your code that you aren't sure how to fix after reading
this article, you can get help! Ask for help on the communication channels. Tell us
what your error is, and we'll try to help you. A listing of your code would be useful as
well.
This page was last modified on Mar 24, 2023 by MDN contributors.
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong 12/12