console.log()A ābugā is a problem in our program:
Debugging can feel pretty frustrating because it means your program isnāt working. But, itās totally normal because programs arenāt working most of the time.
Itās essentially, therefore, to not take bugs personally. They are not a judgement of your abilities as a programmer. The program is broken, not you.
Bugs are great teachers. In exploring and fixing them we learn how programs work and build up our database of common issues and mistakes to avoid.
The code editor is a really useful starting point for debugging. Specifically, it will often show you:
function)Make absolutely sure that you editor is automatically formatting your code.
When coding, you should always have the JavaScript console open in your browser. Open the JavaScript console in Chrome by:
console.log()You can also print information from your program into the console as well. By using console.log(). The key examples areā¦
function myBuggyFunction() {
// Checking if a function is actually being called by having it print
// a message to the console
console.log("myBuggyFunction() called");
// Output in console: myBuggyFunction() called
}
function mySuspiciousFunction() {
// Printing out the values in variables to see if there's something wrong
// like they are "undefined" or "NaN" (not a number) or just the wrong
// kind of thing
console.log(bird.x, bird.y);
// Possible output in Console: undefined 250
}
function mySuspiciousFunction() {
// The same thing, but including some extra text to make it more readable
// in the console window
console.log("bird.x: " + bird.x, "bird.y: " + bird.y);
// Possible output in Console: bird.x: undefined bird.y: 250
}
Errors are bugs in your program that bring up an error message in your JavaScript Console.
A typical error might read like this:
script.js:47 Uncaught (in promise) ReferenceError: setTime is not defined
at setup (script.js:47:5)
at _setup (p5.min.js:3:423972)
at _runIfPreloadsAreDone (p5.min.js:3:423132)
at _._decrementPreload (p5.min.js:3:423304)
at _.<anonymous> (p5.sound.min.js:2:98292)
Here we see:
Uncaught ReferenceError)setTime is not defined)script.js)47)Generally speaking, you only care about finding the line number in a JavaScript file that you wrote. There should not be any errors in p5.js itself.
Uncaught ReferenceError: ______ is not definedJavaScript has found a name in your program that it doesnāt know about. This is generally:
Very often these are caused by typos and you will recognize the mistake quickly if you go to the line in question.
Uncaught SyntaxError: Unexpected end of inputJavaScript reached the end of your program without all the curly brackets matching up.
Automatic formatting is your friend here as you should be able to spot a place where the indentation isnāt quite right (e.g. function definitions not aligned to the left).
Uncaught SyntaxError: Unexpected ____JavaScript ran into something it wasnāt expecting.
This is usually caused by something just before the actual ātokenā it tells you about so keep your eyes open and donāt only look at the line it suggests.
A classic cause is non-matching parentheses. Also not following naming conventions. Or typos on important JavaScript words like let or const or function.
Behavioural issues can be much harder to find. An image might display in the wrong place, a sound might trigger at the wrong time, a character might suddenly disappear from the world, a shape might refuse to move, and much more!
Thereās no easy fix here because we have to spend time figuring out where things are going wrong, asking for help, and building up our knowledge of the kinds of things that can happen.
Key starting points are to:
console.log() immediately before it (this will help to catch functions not being called or conditions not being met)console.log() to track what values are in your variables and propertiesWhatever you do, do not become obsessed and debug the same thing for hour after hour.
At the very least take a break to refresh your brain. Even if you just go for a five minute walk before getting back to it, thatās something.
Remember youāre learning and ask for help if itās taking a bit too long to find the problem. Itās crucial to try yourself, but you also donāt want to lose all your time to something someone else can easily help with.