Common Errors in JavaScript

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
StringUpper/Lower Case operation onInteger
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.



