Error Types In JavaScript

Β·

3 min read

Error Types In JavaScript

"πŸ’» Javascript: The πŸ€” Reference Error and 🀨 Type Error - The Odd Couple of Coding 🀣"

We all faced an error in our Javascript code and thought to ourselves, "what the heck is going on?" Well, chances are it's either a Reference Error or a Type Error. These two errors are like an odd couple in coding, always causing trouble and making us pull our hair out 🀯. But don't worry, I am here to explain these errors in a fun and easy way, to help! πŸŽ‰

Reference Error

Reference Error: The πŸ€” Forgetful One

Reference errors are like forgetful friends who can never remember your name πŸ€¦β€β™€οΈ. In Javascript, a Reference Error occurs when we try to access a variable that hasn't been declared. It's like trying to play a football game without a ball πŸ€”.

If we try to access a variable "x" that hasn't been declared, we'll get a Reference Error.

console.log(x); 
// ReferenceError: x is not defined πŸ’₯

To avoid this error, make sure you've declared your variables before using them.

It's as simple as introducing yourself before starting a conversation πŸ—£οΈ.

Type Error

Type Error: The 🀨 Picky Eater

Type errors are like picky eaters (who only like to eat certain foods )πŸ”. In Javascript, a Type Error occurs when you try to operate on the wrong data type.

It's like making sure we have the right phone number before making a call πŸ“ž.

If we try to perform a mathematical operation on a string, we'll get a Type Error.

let x = "hello";
let y = x + 2; 
// TypeError: cannot convert a string to a number πŸ€”

To avoid this error, make sure the data type of your variables is appropriate for the operation you're trying to perform.

If we want to add two numbers, make sure both operands are numbers:

let x = 42;
let y = x + 2; 
console.log(y); 
// 44

In the example above, we declared x as a number and added 2 to it. Now when we log y to the console, we get the expected result of 44.

When we try to call an object that isn't a function as a function. It's like trying to call a pencil a phone πŸ€”. In other words, Javascript can't call the object we're trying to use as a function because it's not a function.

let x = "hello";
x(); 
// TypeError: x is not a function πŸ’₯

In the example above, we're trying to call x as a function, but x is a string. This results in a Type Error, with the message "x is not a function."

It's like trying to bake a cake with salt instead of sugar 🀒.

To avoid this error, make sure the object we're trying to call as a function is a function.

If we want to call a function, make sure the object we're calling is a function:

let x = function() { 
console.log("Hello, World!"); 
};
x(); 
// Hello, World!

In the example above, we declared x as a function and called it. Now when we run the code, we get the expected output of "Hello, World!".

It's like checking the ingredients before making a recipe 🍲.

So, remember: Before you try to call an object a function, make sure it's a function! πŸ’‘ Avoiding Type Errors will save you time and make your code run smoothly πŸš€.

Reference Errors and Type Errors may seem like causes of trouble, but they help point out the mistakes in our code. So, the next time you come across one of these errors, just think of them as funny little reminders to double-check your code πŸ”.

Happy coding! πŸ’»πŸŽ‰πŸ˜Š sab haule haule ho jayega Tanay Pratap bhaiya hai na😊.

Β