curriculum/challenges/english/blocks/review-debugging-javascript/6723cd54fc196dbd053f9dfb.md
const arr = ["Beau", "Quincy" "Tom"]
let or const, before it has been defined.:::interactive_editor
console.log(num);
const num = 50;
:::
const developerObj = {
name: "Jessica",
country: "USA",
isEmployed: true
};
developerObj.map()
const arr = [];
arr.length = -1;
throw Statementthrow statement in JavaScript is used to throw a user defined exception. An exception in programming, is when an unexpected event happens and disrupts the normal flow of the program.:::interactive_editor
function validateNumber(input) {
if (typeof input !== "number") {
throw new TypeError("Expected a number, but received " + typeof input);
}
return input * 2;
}
console.log(validateNumber("Naomi")); // TypeError: Expected a number, but received string
:::
try...catch...finallytry block is used to wrap code that might throw an error. It acts as a safe space to try something that could fail. The catch block captures and handles errors that occur in the try block. You can use the error object inside catch to inspect what went wrong. The finally block runs after the try and catch blocks, regardless of whether an error occurred. It's commonly used for cleanup tasks, such as closing files or releasing resources.:::interactive_editor
function processInput(input) {
if (typeof input !== "string") {
throw new TypeError("Input must be a string.");
}
return input.toUpperCase();
}
try {
console.log("Starting to process input...");
const result1 = processInput("hello");
console.log("Processed result:", result1); // HELLO
const result2 = processInput(9); // throws TypeError
console.log("Processed result:", result2); // not executed
} catch (error) {
console.error("Error occurred:", error.message);
}
:::
debugger Statement: This statement lets you pause your code at a specific line to investigate what's going on in the program.:::interactive_editor
let firstNumber = 5;
let secondNumber = 10;
debugger; // Code execution pauses here
let sum = firstNumber + secondNumber;
console.log(sum);
:::
console.dir(): This method is used to display an interactive list of the properties of a specified JavaScript object. It outputs a hierarchical listing that can be expanded to see all nested properties.console.dir(document);
console.table(): This method displays tabular data as a table in the console. It takes one mandatory argument, which must be an array or an object, and one optional argument to specify which properties (columns) to display.Review the Debugging JavaScript topics and concepts.