Important features of ES6 
Published: 2021-05-06 15:18:52

                           Arrow function

Arrow function is an alternative to traditional function. The syntax of an arrow function is functionName=(parameters)=>{function body}.
This is a part of ES6. The advantages of the arrow function over the traditional function are:
1.The syntax allows us an implicit return when there is no body block. This feature simplifies our code.
2.It automatically binds this to the surrounding code's context.
3. It is shorter and simpler.
The main difference between the arrow and the traditional function is in regular function this keyword represents the object that called the function but in the arrow function, this keyword represents the object that defines the arrow function.


                         Error handling

Sometimes errors occur to our code because of our mistake, invalid user response, and erroneous server response. These errors stop our code execution. Error handling allows us to handle errors effectively so that it does not stop our code execution. Error handling is used to deal with run-time errors, not with syntax errors. Error handling is done by introducing a try-catch block.

                         try-catch block

A try-catch block is used in error handling to deal with runtime errors. The code that may produce errors is written in the try block. If an exception occurs in the try block an error is thrown and the catch block process this error. The try and catch block must be written in the same function in order to catch an exception inside a scheduled function. Execution of try-catch blocks are given below:
1.First, the code in try {...} is executed.

2.If there were no errors, the execution reaches the end of the try block and goes on, skipping catch block

3.If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err). The err variable (we can use any name for it) will contain an error object with details about what happened.

                try-catch only works for runtime errors

The error that occurred during execution is called runtime errors. The main purpose of try-catch block is to make the code runnable that means even if error occurred in runtime the code will execute properly.

                   try-catch works synchronously

If a programmer used setTimeOut method, then try catch won't catch it. For example,
try{
   SetTimeout(function(){
       let division=x/y;
   },1000)
}
catch (err){
  alert("didn't work");
}

But, if you want to catch an exception inside a function try-catch must be used.

   SetTimeout(function(){
       try{
           let division=x/y;
       }
       catch{
             alert("Error occurred!");
       }
   },1000)

                         Error object

When an error has occurred in the try block javascript generates an object containing the details about it. Then the error object is passed to the catch block. For all built-in errors, it has two main properties.

name
Error name. For instance, for an undefined variable that’s "ReferenceError".
message: Textual message about error details.

There are other non-standard properties available in most environments. One of most widely used and supported is:

stack
Current call stack: a string with information about the sequence of nested calls that led to the error. Used for debugging purposes.

                       
                       Throwing our own errors

We can throw our own customize error by using the throw operator. The syntax is: throw <error object>. Technically, we can use anything as an error object. That may be even a primitive, like a number or a string, but it’s better to use objects, preferably with name and message properties.JavaScript has many built-in constructors for standard errors: Error, SyntaxError, ReferenceError, TypeError, and others. We can use them to create error objects as well.

Their syntax is:

let error = new Error(message);
let error = new SyntaxError(message);
let error = new ReferenceError(message);
For example,
let error = new Error("This is a custom error");
alert(error.name); // Error
alert(error.message); // This is a custom error

                       instanceof operator

To check the error type instanceof operator is used. For example,
try{
    let division= x/y;
}
catch(e){
   if(e instanceof ReferanceError){
      alert('Error has occured');  
   }
}

                       conditional catch-block

Conditional catch-block is implemented by using if else inside the catch block. For example,

try {
  myroutine(); // may throw three types of exceptions
} catch (e) {
  if (e instanceof TypeError) {
    // statements to handle TypeError exceptions
  } else if (e instanceof RangeError) {
    // statements to handle RangeError exceptions
  } else if (e instanceof EvalError) {
    // statements to handle EvalError exceptions
  } else {
    // statements to handle any unspecified exceptions
    logMyErrors(e); // pass exception object to error handler
  }
}

                     The exception identifier

When the exception was thrown the exception variable holds the exception value. The identifier is used to get the information about the exception and is only accessible in the catch block. If we don't need the exception value it could be omitted.

                       The finally block

The finally-block contains statements to execute after the try-block and catch-block execute, but before the statements following the try...catch...finally-block. Note that the finally-block executes regardless of whether an exception is thrown. Also, if an exception is thrown, the statements in the finally-block execute even if no catch-block handles the exception.