Beginner way of Solving JavaScript Error, try, catch and debugging

Code or program always doesn’t run the way we want them to run. In our programming life we have to face error too often. Today I am trying to share my error handling and debugging strategy. And I also try to explain try and catch block of JavaScript in a beginner way. Without any further ado, let’s get to the point.

Strategy 1. Rewrite code block:

Most of the times beginner programmer made typing mistake and could not find why their code isn’t working. I face this problem many times, but it is not easy to find where I made that mistake. Later I started to rewriting the code block and to able to find mistake easily. I might be unable to explain this clearly to you. Follow the code block to get a clearer view.

So Here I start to rewrite this code this way-

This example may be not that great, but you can start this way for complex function too.

Strategy 2. console.log()

When we write a function with several steps, and get a error, we beginner often failed to where we made the mistake. In fact, finding our own mistake is the hardest things to do. So, I use a lot of console.log() function to avoid and find the mistake. Here is a code example trying to make it easier to understand.

Here our expected out is the second one, but the function would never return the second condition? Why? As a beginner you might have to face similar situation. Specially working with condition.

JavaScript try…catch:

Sometimes we make mistake or our user give wrong input or there could thousand other reason to break our scripts. JavaScript try and catch are there to handle this error.

JavaScript Types:

There is a proverb, ‘Everything in JavaScript is a Object’. Although it is not absolutely correct, it is almost correct. There are two main types in JavaScript. Primitive and Object.

  1. Primitive Values:

There are just seven primitive values in JavaScript. They are —

a. Undefined: (undefined) used for defining some missing values

b. Null: (null) also used for intentionally missing values.

c. Booleans (True and False): This two values used for logical value

d. Numbers (10, -25, 3.5, 0): Numbers types used for math operations.

e. Strings (‘alohomora’, ‘hello’): used for text information.

f. Symbol (unknown): This type used to hide implementation details.

g. Bigint (unknown and new): This is used for math big numbers.

2. Object:

Except those above items, everything in JavaScript is Object (and function type).

a. Object: Object {} and others (Array) are used to group related data and functions.

b. Function: const double = num => num * 2 is used to refer code.

Function Default Parameter:

Many times we need to use parameters in function. We can set a default value for those parameters too. Here is an example.

Spread Operator:

Spread Operator (…) or rest operator use to spread or copy properties of an object or an array.

You can used these to de-structure, and copy object easily.

Quick Introduction of ES6:

ES6 or Ecma Script 6 is the JavaScript version of 2015. Here are some of the main feature of ES6.

  1. var, let, const: In ES6, var is replaced with two keyword. let and const. const variable is immutuable. You can not reassign it after declaring. On the other let is used to declair a varibale.

2. Arrow Function:

ES6 introduced us arrow function. Which is the shortest way to declare a function.

In one line, you would not need to use return keyword to return something from a arrow function.

3. Block level Declaration and Binding: In ES6, JavaScript allows us to declare block level variable. We can declare some variable inside a function and it won’t be available outside the function.

That’s all for today. Wish to see you in another blog in another day. Until then keep calm, focused and healthy.

MERN Stack (JavaScript) Web Developer