JavaScript try…catch and ES6 features Block Bindings and Function

Imnulhaqueruman
3 min readMay 6, 2021

1.When we write code, sometimes our scripts have errors. They may occur because of our mistakes, unexpected user input and server response. As a result, the script dies in case of error printing it to console.

But there is a syntax try…catch for catch errors instead of script dies.

The try...catch construct has two main blocks: try, and then catch:

try {  // code...} catch (err) {  // error handling}

2.Coding Style

Our code must be as clean and easy to read as possible.

Here the cheat sheet for suggestions

In most JavaScript projects curly braces are written in “Egyptian” style with the opening brace on the same line as the corresponding keyword — not on a new line. There should also be a space before the opening bracket, like this:

if (condition) {
// do this
// ...and that
// ...and that
}

Semicolon

A semicolon should be present after each statement, even if it could be skipped.

There are languages where a semicolon is truly optional and it is rarely used. In JavaScript, though, there are cases where a line break is not interpreted as a semicolon, leaving the code vulnerable to errors. See more about that in the chapter Code structure.

If you’re an experienced JavaScript programmer, you may choose a no-semicolon code style like StandardJS. Otherwise, it’s best to use semicolons to avoid possible pitfalls. The majority of developers put semicolons.

3.Comments

When we write code here we write many function and many objects. When anyone or we read this code later some things we cannot understand easily for a result we rewrite the same code.But if we use the comment in code that is helpful for us to read the code.

comments can be single-line: starting with // and multiline: /* ... */.

An important sign of a good developer comment: their presence and even their absence.

4.What is Binding?

When we declare a variable, we actually bind a value in a name that is known as binding .so binding occurs when we declare a variable with using keywords var, let, const in JavaScript.

5.What is Hoisting?

ES6 brings a new feature called hoisting. In general, hoisting is a mechanism that handles execution contexts in JavaScript. This means the variable and function declarations (not initializations) are put into the memory during the compile phases before going for any execution.

6.Block Level Declarations

If we declare any variable using const and let in a block-level scope we cannot access this declaration outside block-level scopes. There are two types we can be created block-level scopes :

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

7.“Let” Declarations

function func1()
{
for (let i = 0; i < 10; i++)
{
let tmp = 10;
}

console.log(tmp); // would result in an error
}

The let declarations give us truly in this loop block-scope

8.“Const” Declaration

When once we declared a variable with a const declaration variable can not be modified.

const pi = 3.14;

pi = 5; // error

The constant variable is also block-level variable similar to let .

9.Default parameters in Function

In JavaScript function, we can take parameters but In JavaScript, function parameters default to undefined

function multiply(a, b) {
return a * b
}

multiply(5, 2) // 10
multiply(5) // NaN !

In the following example, if no value is provided for b when multiply is called, b's value would be undefined when evaluating a * b and multiply would return NaN.

so we want to avoid this error we can declare the default value in b parameter .

Like as

function multiply(a, b = 1) {
return a * b;
}

console.log(multiply(5, 2));
// expected output: 10

console.log(multiply(5));
// expected output: 5

10.Arrow function expressions

An arrow function expression is a compact alternative to a traditional function expression, but is limited and can’t be used in all situations

Arrow functions were introduced in ES6.

Arrow functions allow us to write shorter function syntax:

One param. With simple expression return is not needed:

param => expression

Multiple params require parentheses. With simple expression return is not needed:

(param1, paramN) => expression

Multiline statements require body brackets and return:

param => {
let a = 1;
return a + param;
}

--

--