Sitemap

Errors In Javascript

1 min readApr 1, 2024

--

1. ReferenceError:

This error occurs when you try to use a variable or function that hasn’t been declared yet, or it’s misspelled. The program cannot find a reference to the element you’re trying to use.

  • Example:
// This will cause a ReferenceError because "message" is not declared
console.log(message);

2. SyntaxError:

This error occurs when there’s a mistake in the structure or grammar of your code. It violates the rules of the programming language you’re using.

    Get Sushant kumar’s stories in your inbox

    Join Medium for free to get updates from this writer.

  • Example:
// Missing semicolon after the variable declaration will cause a SyntaxError
let name = "Alice"
console.log(name)

3. TypeError:

This error occurs when you try to perform an operation on a value that isn’t the expected type. For example, trying to add a string and a number. The program encounters a type mismatch.

  • Example:
// Trying to add a string and a number will cause a TypeError
let age = 25;
let name = "Bob";
console.log(age + name); // This will result in a TypeError

Here’s a table summarizing the key differences:

--

--