Errors in JavaScript
What are errors?
Errors in JavaScript are like spelling mistakes in your favorite comic book; they make the story go haywire. But in the world of coding, we call them “bugs” or “errors.” Errors happen when the computer can’t understand your instructions because you’ve made a mistake in your code.
Can you spot the error in this script below?
console.log("Hello, world!";
Cell In[3], line 1
console.log("Hello, world!";
^
SyntaxError: invalid syntax
Oops! We forgot to close the parentheses at the end of the console.log() function. Computers are very picky about syntax. To fix this, just add the missing ) like so:
console.log("Hello, world!");
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/Users/torinwolff/Documents/GitHub/NART_Movie/_notebooks/2023-09-27-errors.ipynb Cell 5 line 1
----> <a href='vscode-notebook-cell:/Users/torinwolff/Documents/GitHub/NART_Movie/_notebooks/2023-09-27-errors.ipynb#W4sZmlsZQ%3D%3D?line=0'>1</a> console.log("Hello, world!");
NameError: name 'console' is not defined
Types of Errors
There are three main types of errors in JavaScript: Reference errors Syntax errors Type errors
Reference Errors
Reference errors occur when you try to use a variable that doesn’t exist. For example:
let name = "Alice";
console.log(alice);
The script above will throw a reference error because the variable “alice” is not defined. To fix this, we need to switch alice with the known variable name.
let name = "Alice";
console.log(name);
Type Errors
Type errors in JavaScript are errors that occur when the type of a variable or expression does not match the type that is expected. For example, if you try to add a string to a number, you will get a type error.
Here are some examples of type errors in JavaScript:
// Type error: trying to add a string to a number
var x = "1";
var y = 2;
x + y; // Error: Type error: Cannot implicitly convert type 'string' to 'number'
// Type error: trying to access a property that does not exist
var obj = {
name: "John Doe"
};
obj.age; // Error: Type error: Cannot read property 'age' of undefined
// Type error: trying to call a method that does not exist
var myObj = {
sayHello: function() {
console.log("Hello!");
}
};
myObj.sayGoodbye(); // Error: Type error: Cannot call method
Type errors can be avoided by using the correct types for variables and expressions. For example, you can use the typeof operator to check the type of a variable or expression. Type errors can also be avoided by using type annotations. Type annotations are a way of specifying the types of variables and expressions. Type annotations can be used to improve the readability and maintainability of your code. They can also help to prevent type errors from occurring.
Syntax Errors
Syntax errors occur when you try to execute code that does not follow the rules of the JavaScript language. For example, if you try to use a reserved word as a variable name, you will get a syntax error.
var x = 10;
y = 20;
In this example we are trying to access a variable x but we used an invalid variable to change it as seen in x and y. So js will throw a syntax error.
Hacks
Try/Catch
The try/catch statement allows you to test a block of code for errors. The try block contains code that might throw an error. The catch block contains code that handles the error. The finally block contains code that is always executed, regardless of whether an error occurs or not. Here is an example of a try/catch statement:
try {
// Code that might throw an error
} catch (error) {
// Code that handles the error
} finally {
// Code that is always executed
}
Throw
The throw statement allows you to create custom errors. Here is an example of a throw statement:
throw new Error('This is a custom error message');