Sharpen your javascript skills with these topics
If you are a novice, you will hate errors. If you are an expert, you will enjoy the errors because error leads you to make your program nearly flawless. In this topic, we learn more about Error Handling using try-catch block that may help you to become a great programmer 😉
Javascript is an interpreted programming language. A developer or programmer, like every other language, must often consider error handling. Typically, a programmer or developer may handle errors when accessing or assigning data to a database. As a result, error handling is an essential part of every programming language. A programmer or developer often faces three types of programming errors.
Syntax Error — A error in writing code that violates the syntax of a programming language. For instance, by ignoring a semicolon or the convention of declaring and calling function.
Logical Error — An error in logic formation. For example, for applying the incorrect arithmetic operation, resulting in the wrong output.
Runtime Error — An error occurred during the execution of the program. By calling a function without declaring it, For example.
Try / Catch
Try/Catch is used to handle Runtime Error
Syntax
The syntax for a try-catch block is common and straightforward. In Javascript, the try-catch block is usually being declared like this
try {
//code to try or test
} catch (error) {
// code after getting an error
}
How does it work?
To begin testing, we write some lines of code in the “try” block. If the code is run or the evaluation is passed successfully. The “try” block would not throw any errors to the “catch” block and will instead perform the “finally” block. Otherwise, it would throw an exception to the “catch” block, where we will manage the exceptions based on the mistake. Using the “throw” keyword, we may also throw a custom error to the “catch” block.
Try/Catch with finally
The try statement is a block of code that requires checking the code and where exceptions exist, which thrown using the throw statement. If any mistakes are thrown by the throw statement, they are handled by the catch statement. Finally, the user is allowed to run the code.
Error Throwing
When an error happens, javascript will interrupt and generate an error message, indicating that javascript has thrown an error. The throw declaration allows users to make a personalized error, meaning that javascript has thrown an exception. These exceptions may take the form of a javascript string, a Boolean value, a number, or an object. The user can manage the program flow and create custom error messages by using try and catch statements.
Syntax
var x = 7, y = 7;try {
if(x == y){
throw "errorOne";
}else{
throw "errorTwo";
}
} catch(e) {
if(e == "errorOne"){
console.log("Same")
}
if(e == "errorTwo")
console.log("Different)
}
Global Catch
If we want to catch an Error outside of the try-catch block, we have a solution that is called global catch
Destructuring
Destructuring a JavaScript expression that lets us extract information from arrays, objects, maps, and sets. It is a quick but efficient method for extracting variables out of array fragments or object properties — this technique typically results in simpler and more readable code. Let’s look at an example
const name = ['Shahriar', 'Emon'];const [ firstName, lastName] = name;console.log(firstName); //Shahriarconsole.log(lastName); //Emon
Arrow Function
Arrow functions let us easily define JavaScript functions with or without parameters by using the fat arrow => operator. To write shorter function syntax, we can ignore the curly braces as well as the function and return keywords while creating a new JavaScript function. They not only save time, but they also offer cleaner and more readable functions.
// No parameters
var myFunc = () => {
// Do stuff
}; // With parameters
var myFunc = (a, b) => {
return a * b;
}; // Only 1 param, no need for parens
var myFunc = a => {
return a * a;
}; // No brackets and implicit return
// with single-line arrow function
var myFunc = a => a * a; // Wrap body in parens to return
// object literal
var myFunc = (first, last) =>
({ firstName: first, lastName: last });
Async
Async functions let us write promise-based code in the same way that synchronous code does, but without blocking the execution thread. The event-loop allows it to run asynchronously. A value is often returned by async functions.
Await
Await can only be used within a Function block where Async is used, it’s only valid in Async Function. The main purpose of the await is to wait for a promise. When Await operator is used it will wait until a result is returned by the promise.
Event Loop
JavaScript is a single-threaded programming language. That is, it can only do one task at a time and cannot do anything else while doing the task. That is where Event Loop comes in handy. The Event Loop controls the Call Stack and the Callback Queue. When the Call Stack is empty, it takes the first case in the queue and pushes it to the stack, where it is executed.