Skip to main content

Command Palette

Search for a command to run...

Common Errors in JavaScript

Updated
•2 min read•View as Markdown
Common Errors in JavaScript
A

Frontend Developer 🧑‍💻

Syntax Error

A Syntax error is caused due to incorrect usage or a typo in your code.
Some examples include -

  • Missing or invalid opening/closing Brackets [...], Parentheses (...) or Curly braces {...}
function printNum(num) {
  console.log(num);

  • Missing or invalid semicolons " or colons '
console.log("Abhishek');

  • Typo in variable name or function.
comst myName = "Abhishek";


Reference Error

A Reference error occurs when a variable or a function is used that does not exist or is out of scope. For example -

  • Accessing a variable before Declaring.
console.log(a);

  • Accessing a variable before Initializing.
console.log(age);
let age = 23;

  • Declaring a variable without a variable name in strict mode
"use strict"
age = 23;
console.log(age);

  • Out of Scope variable
function getUserInfo() {
  const userName = "Abhishek";
}
console.log(userName);


Type Error

Type error occurs when some value doesn’t turn out to be of a particular expected type. For example -

  • Using String Upper/Lower Case operation on Integer
const age = 20;
console.log(age.toUpperCase());

  • Calling variables as functions
let userInfo;
userInfo();

  • Updating a value that cannot be changed
const num1 = 10;
num1 = 20;


There are many ways an error can happen but these are some common types of errors and how they occur. Hope you learned something and thanks for reading.