DEV Community

loading...

Important features of ES6

sarman7105 profile image Sarman Khurshid Alam Updated on ・4 min read
                       Arrow function
Enter fullscreen mode Exit fullscreen mode

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.

  1. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

We can throw our own customize error by using the throw operator. The syntax is: throw . 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Discussion (0)

Forem Open with the Forem app