Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Reading Errors

Mistakes are a normal part of programming. Even experienced programmers make them every day. What matters is finding and fixing them quickly, and error messages help you with that.

When JavaScript can't run your program, it shows a red error message below the output. Read it carefully: it has two parts. For example:

ReferenceError: prise is not defined
  • Before the colon is the type of the error, here ReferenceError.
  • After the colon is a description of what went wrong. This one says that prise isn't defined, so JavaScript doesn't know this name.

The most common types of errors are:

  • SyntaxError: the code breaks the rules for writing JavaScript, its syntax. Usually a quote or a parenthesis is missing. For example, console.log("Hi"; ends with SyntaxError: missing ) after argument list, which says that a closing parenthesis is missing. A missing quote gives SyntaxError: Invalid or unexpected token. JavaScript checks the whole program before running it, so when there is a syntax error, nothing runs at all, not even the lines before the mistake.
  • ReferenceError: you used a name that JavaScript doesn't know. Usually it's a typo (prise instead of price, or Price, because uppercase and lowercase letters matter), a variable used before it was created, or text without quotes: console.log(Anna).
  • TypeError: you tried to do something that the value doesn't allow, such as changing a constant: TypeError: Assignment to constant variable.

With a ReferenceError or a TypeError, the program runs until it reaches the mistake, and then it stops. What it printed before stays in the output. That's a useful clue: the mistake is after the last console.log() that still printed something.

The descriptions here are the ones Chrome shows. Other browsers word them a little differently, but the type of the error is the same everywhere.

When you get an error, read its type and description, find the place in the code it's about, and fix it. Then run the program again: there may be another mistake further on.

Instructions

  1. 1

    The program in the editor has two mistakes. Run it, read the error message, fix the mistake and run the program again. Repeat this until the program prints:

    Player: Anna
    Points: 10
    Points after the bonus: 15
Back

14/15

  1. 1. Printing
  2. 2. Arithmetic Operations
  3. 3. Comments
  4. 4. Variables
  5. 5. Changing a Variable's Value
  6. 6. Quiz: The Order of Commands
  7. 7. Constants
  8. 8. Division and Remainder
  9. 9. Joining Text
  10. 10. Text and Numbers
  11. 11. Quiz: Text and Numbers
  12. 12. Template Literals
  13. 13. Quiz: Template Literals
  14. 14. Reading Errors
  15. 15. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.