Lokang 

JavaScript and MySQL

Types of errors

There are several types of errors that can occur in JavaScript. Here are some of the most common ones:

Syntax errors: These occur when you have a mistake in your code that violates the rules of the language. For example, forgetting to close a quote, missing a semicolon, or using a reserved keyword.

// Example of a syntax error
let name = "John"; // Missing semicolon
console.log(name);

Runtime errors: These occur during the execution of your code. For example, when you try to access a variable that is not defined or when you call a method that does not exist.

// Example of a runtime error
let x;
console.log(x.toUpperCase()); // "toUpperCase" method does not exist for "undefined"

Logical errors: These occur when your code does not produce the expected result, but it runs without generating an error. These types of errors can be difficult to detect and fix.

// Example of a logical error
function calculateTotal(price, quantity) {
 return price * quantity; // Forgot to add tax
}
let total = calculateTotal(100, 2);
console.log(total); // Output: 200 (but it should be 220 with 10% tax)

It's important to handle these errors properly in your code, so that you can provide a good user experience and prevent your application from crashing. One way to do this is to use a try-catch statement, which allows you to catch and handle errors gracefully.

// Example of a try-catch statement
try {
 let x;
 console.log(x.toUpperCase());
} catch (error) {
 console.log("An error occurred: " + error.message);
}

In this example, the code inside the try block generates a runtime error, but the catch block catches the error and logs a message to the console, preventing the application from crashing.