Diving deep into JavaScript errors.

Diving deep into JavaScript errors.

Introduction

I vividly recall my initial encounter with an error message. It was not a negative experience, but rather puzzling due to the abundance of obscure terms and sentences displayed on the screen. However, those mysterious and unusual lines allowed me to explore new avenues of learning.

Errors occur when we violate programming language rules or attempt to place unsuitable value types in unsupported locations, among other reasons. In this blog post, I'll delve deeper into the three primary types of errors that occur in JavaScript: ReferenceError, TypeError, and SyntaxError.

ReferenceError

function referenceErrorTest(){
    console.log(myVariable)
}
referenceErrorTest()
// OUTPUT:
// Uncaught ReferenceError: myVariable is not defined

The term "ReferenceError" may appear straightforward, but for those still unsure about the outcome of the code snippet provided, here's an explanation. The code defines a function called "referenceErrorTest," which attempts to log the value of a variable named "myVariable" to the console. However, the code never declares or defines the "myVariable" variable anywhere, causing a reference error to occur. In simpler terms, the code throws a reference error because the function tries to access a variable that doesn't exist. The ReferenceError means referencing a variable which is either doesn't exist, hasn't been initialized or is not in the scope.

TypeError

let a = null
a.name
// Uncaught TypeError: a is null

const x = 10
x()
// Uncaught TypeError: x is not a function

let a = 10
for(let item of a){
    console.log(item)
} 
// Uncaught TypeError: a is not iterable

TypeError can be a confusing concept, and it often helps to understand it through real-life examples. Imagine going to an ATM and inserting a card, only to receive no prompt or response. Upon inspection, you realize that you accidentally inserted a gaming card instead of your bank card. To simplify, a TypeError occurs when the type of data you're using doesn't match what is expected by the function or method you're attempting to use. In the case of the ATM example, the gaming card was not recognized as valid data by the machine, resulting in a TypeError.

The aforementioned examples illustrate common scenarios that may result in TypeError in programming:

  • The first example demonstrates that attempting to access a property of a null variable, such as the name property of variable "a", will result in a TypeError.

  • In the second example, defining a variable as an integer and later attempting to use it as a function, as in the case of the variable "x()", will also result in a TypeError.

  • Lastly, using a non-iterable variable in a for...of the loop, such as variable "a" in the third example, will also trigger a TypeError.

Many more scenarios exist, but I have only highlighted a few.

It is important to be aware of such potential errors in programming and to ensure that variables are properly defined and used in a manner that aligns with their intended functionality.

SyntaxError

Certainly, referring to a chair as a table would not be appropriate, just as attempting to use incorrect syntax in programming would result in a SyntaxError.

console.log"hello") 
// Uncaught SyntaxError: unexpected token: string

lit a = 10 
// Uncaught SyntaxError: unexpected token: identifier

The aforementioned examples elucidate the fact that while we may assign a nickname to an individual, such terminology holds no relevance in the realm of computer language. In the digital world, a distinct set of programming rules and syntaxes exist, which must be strictly adhered to. In Simpler terms, SyntaxError shows up when the code violates the programming language's syntax.

I'll conclude on the note that errors may seem haunting or inappropriate at first but they open a lot of quests to search for, making us a veteran.