Debugging is the process of going through your code, finding any issues, and fixing them.
Issues in code generally come in three forms: syntax errors that prevent your program from running, runtime errors where your code has unexpected behavior, or logical errors where your code doesn’t do what you intended.
In this course, you’ll learn how to use the JavaScript console to debug programs and prevent common issues before they happen.
Use the JavaScript Console to Check the Value of a Variable
console.log(a);
Understanding the Differences between the freeCodeCamp and Browser Console
There are many methods to use with console
to output messages. log
, warn
, and clear
to name a few. The freeCodeCamp console will only output log
messages, while the browser console will output all messages. When you make changes to your code, it will automatically run and show the logs. The freeCodeCamp console is then cleared each time your code runs.
console.warn(output);
Use typeof to Check the Type of a Variable
Here are some examples using typeof
:
console.log(typeof "");
console.log(typeof 0);
console.log(typeof []);
console.log(typeof {});
In order, the console will display the strings string
, number
, object
, and object
.
JavaScript recognizes seven primitive (immutable) data types: Boolean
, Null
, Undefined
, Number
, String
, Symbol
(new with ES6), and BigInt
(new with ES2020), and one type for mutable items: Object
. Note that in JavaScript, arrays are technically a type of object.
console.log(typeof three);
Catch Misspelled Variable and Function Names
檢查是否有打錯字
console.log(`Net working capital is: ${netWorkingCapital}`);
Catch Unclosed Parentheses, Brackets, Braces and Quotes
檢查標點符號的位置有沒有打錯
Catch Mixed Usage of Single and Double Quotes
檢查單引號和雙引號是否有混淆使用
Catch Use of Assignment Operator Instead of Equality Operator
記得條件是要使用 === 或是 == ,而不是只使用 =
console.log(result);
Catch Missing Open and Closing Parenthesis After a Function Call
設定完一個function不要忘記要有 ()
console.log(result);
Catch Arguments Passed in the Wrong Order When Calling a Function
要把順序寫正確,你要的數學公式才會正確
console.log(power);
Catch Off By One Errors When Using Indexing
從第0項開始,到最後一項的寫法是,
for (let k = 0; k < len; k++) {
console.log(alphabet[k]);
}
countToFive();
Use Caution When Reinitializing Variables Inside a Loop
修復雙矩陣的寫法
Prevent Infinite Loops with a Valid Terminal Condition
留言