Common JavaScript errors.

Common JavaScript errors.

·

2 min read

JavaScript is among the most common programming languages, it is a powerful and flexible language that is essential for any developer looking to create dynamic and interactive applications. For one to have a smooth and fun time running the programs, one needs to understand the errors that will be thrown.

There are 3 types of major common errors.

  • Syntax based

  • Run time errors

  • Logical errors

Syntax based errors

They are also called parsing errors.

The syntax-based errors are the most common as they amount to 12% of the errors in JavaScript.These occur when the code violates the syntax rules of the JavaScript language. eg Missing of parenthesis as shown below;

script>
   function sayHello(name {
  console.log("Hello, " + name);
}

sayHello("John");



</script>

However, these errors only affect the line of code that has the error.

Runtime errors.

They are also called exceptional errors

These types of errors occur after the program has been interpreted by the compiler. Simply put they occur when the program is run, but there is a problem with the logic of the code. For example, if you try to access a variable that has not been declared or divide by zero, you will get a runtime error.

<script>
function divideByZero(num) {
  return num / 0;
}

console.log(divideByZero(10));
</script>

Logical errors.

They are also called semantic errors.

Logical errors occur when one makes a mistake in the logic, the instructions that you have given were not well interpreted by the program resulting in an unexpected result.

<script>
function calculateAverage(numbers) {
  var sum = 0;
  for (var i = 0; i < numbers.length; i++) {
    sum += numbers[i];
  }
  var average = sum / numbers.length * 100; // This line has a logical error
  return average;
}

console.log(calculateAverage([10, 20, 30]));
</script>