Are you getting frustrated trying to write code that is robust and reliable?
Don't worry, we've all been there! Knowing how to recognize and avoid the most common programming errors can help you save time and headaches in the long run.
In this post, we'll take a look at some of the most common errors in programming, with JavaScript as our case study.
header image from: https://www.zilliondesigns.com/blog/bringing-4xx-user-experience-errors-to-life/
image: (https://hexlet.io/courses/intro_to_programming/lessons/errors/theory_unit)
Most Common Errors in programming are;
- Syntax Errors,
- Type Errors,
- Name Errors,
- Index Errors,
- Value Errors,
- Attribute Errors,
- Key Errors,
- Import Errors,
- IO Errors and
- Runtime Errors.
We'll explain what each of these errors are, how to recognize them, and how to avoid them.
Let's take a look at each of these errors in the context of JavaScript and see how they can be avoided.
Syntax Errors
image from (https://dev-to-uploads.s3.amazonaws.com/uploads/articles/)
Syntax Errors occur when the code written is not valid in the programming language. This can happen due to incorrect punctuation, missing parentheses, or an incorrect keyword. For example, in JavaScript, if you forget to close a bracket, you would get a syntax error.
// Incorrect Syntax
let x = 10
// Correct Syntax
let x = 10;
Type Errors
image: (https://www.google.com/url?sa=i&url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F48839524%2Fhow-to-resolve-javascript-not-a-function-error&psig=AOvVaw0UvRD3YSBVPzr5YXhRQVj5&ust=1673784219719000&source=images&cd=vfe&ved=0CBAQjRxqFwoTCOjx8KCCx_wCFQAAAAAdAAAAABAE)
Type Errors occur when data is used in a way that is not supported by the programming language. In JavaScript, this usually happens when a value is not the correct type. For example, if you try to use a string as an array index, you would get an error.
// Incorrect Syntax
let arr = [1,2,3];
let index = "two";
// TypeError: Cannot read property 'two' of undefined
let val = arr[index];
// Correct Syntax
let arr = [1,2,3];
let index = 1;
let val = arr[index]; // val = 2
Name Errors
Name Errors occur when a variable or function is not defined. In JavaScript, this usually happens when you try to use a variable or function that has not been declared.
// Incorrect Syntax
// ReferenceError: myFunction is not defined
let x = myFunction();
// Correct Syntax
function myFunction() {
return "Hello World";
}
// x = "Hello World"
let x = myFunction();
Index Errors
Index Errors occur when you try to access an element of an array or object that does not exist. In JavaScript, this usually happens when you try to access an index of an array that is out of bounds.
// Incorrect Syntax
let arr = [1,2,3];
// IndexError: Index out of bounds
let val = arr[4];
// Correct Syntax
let arr = [1,2,3];
// val = 3
let val = arr[2];
Value Errors
Value Errors occur when a variable or function is used in a way that is not valid in the programming language. In JavaScript, this usually happens when a value is not of the correct type or is out of range.
// Incorrect Syntax
let x = "Hello";
// TypeError: Cannot convert string to number
let y = x + 1;
// Correct Syntax
let x = "Hello";
// y = "Hello World"
let y = x + " World";
Attribute Errors
Attribute Errors occur when an attribute or property of an object is not valid or does not exist. In JavaScript, this usually happens when you try to access a property of an object that does not exist.
// Incorrect Syntax
let obj = {
name: "John"
};
// TypeError: Cannot read property 'age' of undefined
let age = obj.age;
// Correct Syntax
let obj = {
name: "John",
age: 30
};
// age = 30
let age = obj.age;
Key Errors
Key Errors occur when a key used to access an element of a dictionary or map is not valid or does not exist. In JavaScript, this usually happens when you try to access a key of an object that does not exist.
// Incorrect Syntax
let obj = {
name: "John"
};
// TypeError: Cannot read property 'age' of undefined
let age = obj["age"];
// Correct Syntax
let obj = {
name: "John",
age: 30
};
// age = 30
let age = obj["age"];
Import Errors
Import Errors occur when a module or library is not found or cannot be imported. In JavaScript, this usually happens when you try to use a module or library that has not been installed or is not in the correct path.
// Incorrect Syntax
// Error: Cannot find module 'myModule'
import myModule from "myModule";
// Correct Syntax
import myModule from "./myModule";
IO Errors
IO Errors occur when a file or resource is not found or cannot be read. In JavaScript, this usually happens when you try to read a file that does not exist or is not in the correct path.
// Incorrect Syntax
// Error: ENOENT: no such file or directory
let data = fs.readFileSync("myFile.txt");
// Correct Syntax
let data = fs.readFileSync("./myFile.txt");
Runtime Errors
Runtime Errors occur when an unexpected error occurs while the program is running. In JavaScript, this usually happens when an unexpected value or type is encountered or an unexpected operation is performed.
// Incorrect Syntax
let x = 1;
// TypeError: Cannot convert string to number
let y = x + "Hello";
// Correct Syntax
let x = 1;
let y = x + 2; // y = 3
By understanding what type of errors can occur in programming, and how to recognize and avoid them, you can ensure that your code is robust and reliable. By taking the time to debug your code and check for issues, you can save yourself a lot of time and headaches in the long run.
Hope you enjoyed this piece?
then drop a like or even comment if you find this piece resourceful or want to add more to it.
Also, Kindly connect with me on linkedin and follow me on Twitter, I follow back.
Top comments (0)