How to Try-Catch works and Some Es6 method description?

MOSTAFIZUR RAHMAN ABIR
4 min readMay 6, 2021

It’s helpful for a junior developer

1. try-catch block:

The try statement allows you to define a block of code to be tested for errors while it is being executed.

The catch statement allows you to define a block of code to be executed if an error occurs in the try block.

The JavaScript statements try and catch come in pairs like this:

try-catch statement

2. What is Binding?

Variable is more formally known as binding. When we declare and/or initialize a variable, we actually bind a value to a name inside a scope. Scope usually refers to a specific part of a program.

So binding occurs whenever we declare and/or initialize a variable using one of these keywords: var, let, and const in JavaScript. So, ES6 offers some best approaches for block bindings using var, let, and const.

3. Var Declarations and Hoisting:

Variable declarations using var are treated as if they are at the top of the function (or global scope, if declared outside of a function) regardless of where the actual declaration occurs; this is called hoisting. A simple example for understanding this smoothly:

var declarations and hoisting

4. Block Level Declarations:

If you declare a variable in a block-level scope using any of let and const you can not access the declaration outside that block scope. Block Scopes can be created in the following places:

  • Inside a function (function block)
  • Inside a block (wrapped with two curly { } braces)

We have already seen these block-level scopes in the example above. Now we will use the previous function again but this time using either let or const. Ok. I am going to use let. Consider the following example:

block level declarations

Notice the let binding. This time name only exists inside the if block. It is not available outside the if block. Because the JavaScript engine does not hoist let and const declarations. So the name is not accessible in else and enclosing blocks.

5. Block Bindings in Loops:

In JavaScript, frequently web designers need block-level checking of factors is inside for loops. For, this we have to use let not var, cause var is being hoisted. Follow the two examples below:-

So in this example, j is accessible outside of the loop because we use var and

here k is not accessible because we use let. So, it will throw an error.

6. Global Block Bindings:

We know let and const are different from var in their global scope behavior.So, when var is used in the global scope, a new property is added to the global object (window in browsers). That means you can overwrite an existing global using var, such as:

After that, if you use let or const in the global scope, a new binding is created in the global scope but no property is added to the global object. So, here also you can not overwrite a global variable using let or const. Example:

7. Block-Level Functions:

The new features in ES6 is block-level Functions. It allows block-level functions which are hoisted on top of the function or hoisted into the global scope. Here is an example:-

8. Arrow Functions:

Arrow function is one of the features introduced in the ES6 version of JavaScript. It allows you to create functions in a cleaner way compared to regular functions. That means, we can write a functions in a shorter way. Look at that example:-

9. Spread Operator:

The spread operator is a new addition to the features available in the JavaScript ES6 version. It allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) are expected.

10. Functions with default Parameters:

In ES5 and earliar version, we have to set functions all parameter value formally. If we somehow forget to set any parameter value, the function immediately stopped and showing error. This problem solved in ES6 version. Here, we can set a default value in a parameter. In this case, if we not set on that parameter value, the function execute the default value as its parameter. Example:-

--

--