JavaScript Core Concepts

Mikat Syed
5 min readMay 8, 2021

JavaScript try…catch…finally Statement

1. Types of Errors

In programming, there can be two types of errors in the code:

Syntax Error: Error in the syntax. For example, if you write, the above program throws a syntax error. The spelling is a mistake in the above code.

Runtime Error: This type of error occurs during the execution of the program. For example,
calling an invalid function or a variable.

Error Handling

2.try…catch

A try…catch is a commonly used statement in various programming languages. Basically, it is used to handle the error-prone part of the code. It initially tests the code for all possible errors it may contain, then it implements actions to tackle those errors (if occur). A good programming approach is to keep the complex code within the try…catch statements.

try{} statement: Here, the code which needs possible error testing is kept within the try block. In case any error occurs, it passes to the catch{} block for taking suitable actions and handle the error. Otherwise, it executes the code written within.

catch{} statement: This block handles the error of the code by executing the set of statements written within the block. This block contains either the user-defined exception handler or the built-in handler. This block executes only when any error-prone code needs to be handled in the try block. Otherwise, the catch block is skipped.

Syntax :

try{

expression; } //code to be written.

catch(error){

expression; } // code for handling the error.

Example :

try {

alert(‘Birds are fly in the sky’); // (1) ←

// …no errors here

alert(‘End of try runs’); // (2) ←

}

catch (err) {

alert(‘Catch is ignored, because there are no errors’); // (3)

}

output :

Birds are fly in the

sky End of try runs

3.Throw Statement :

The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won’t be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate

Syntax :

try{

throw exception; // user can define their own exception

}

catch(error){

expression; } // code for handling exception.

Example :

const number = 40;
try {
if(number > 50) {
console.log('Success');
}
else {

// user-defined throw statement
throw new Error('The number is low');
}

// if throw executes, the below code does not execute
console.log('hello');
}
catch(error) {
console.log('An error caught');
console.log('Error message: ' + error);
}

output :

An error caught
Error message: Error: The number is low

4.Finally Statement :

finally() The finally() method returns a Promise . When the promise is settled, i.e either fulfilled or rejected, the specified callback function is executed. This provides a way for code to be run whether the promise was fulfilled successfully or rejected once the Promise has been dealt with.

Syntax :

try {
try_statements
}
catch (exception_var) {
catch_statements
}
finally {
finally_statements
}

Example :

const numerator= 100, denominator = ‘a’;

try {

console.log(numerator/denominator);

console.log(a);

}

catch(error) {

console.log(‘An error caught’);

console.log(‘Error message: ‘ + error);

}

finally {

console.log(‘Finally will execute every time’);

}

output :

NaNAn error caughtError message: ReferenceError: a is not definedFinally will execute every time

Comment

5.What Is Comment?

The comment is a readable explanation for the source code of any computer program. Comment used to make the source code easier for other humans to understand that properly.

6.Why Comment Is Use?

· Comments make code more readable and understandable.

· Useful for a large team with lots of people.

· Recall projects part after a log of time.

· Add some design to your project’s code.

· It will attract the person who is watching your codes.

7.Types of comments

There are two types of comments.
1. Single-Line Comments
2. Multi-line Comments

Single-Line Comments

Single line comments start with //.

Any text between // and the end of the line will be ignored by JavaScript (will not be executed).

This example uses a single-line comment before each code line:

//document.getElementById(“test_el”).innerHTML = “Some text”; document.getElementById(“test_el2”).innerHTML = “Some more text”;

Multi-line Comments

Multi-line comments start with /* and end with */.

Any text between /* and */ will be ignored by JavaScript.

This example uses a multi-line comment (a comment block) to explain the code:

/* JavaScript code */ document.getElementById(“test_el”).innerHTML = “Some text”; document.getElementById(“test_el2”).innerHTML = “More text”;

8.Default Parameters in ES6

In ES6 function can have a default parameter. When a function is called and demands some parameter if we do not give the demanded parameter it will give the output NaN. In ES6 we can give a default parameter so if any of the values of the parameters is not give in the function call it will give the output for the default parameter.

9. Template Literals in ES6

Template literal is a way to create a string. It’s a greater way to write dynamic strings whereas previously we were using Concat() method to create a dynamic string.

10. Multi-line Strings in ES6

Back then we needed to use Concat() method and newline character /n for writing multiline string but in ES6 we simply utilize this with a backtick.

11. Destructuring Assignment in ES6

Objects and arrays are the two most used data structures in Javascript. This Destructuring assignment helps to uncrate the object and arrays into a collection of variables that are easier to handle and work with. It also can be used in functions to reduce the complexity of the functions. Repetition of code is very much decreased by this feature.

12. Proper tail-calls

A function is called tail-recursive if the recursive call occurs in the portion of any function. These tail-recursive functions perform better than any other recursive functions. The improved tail-recursive call doesn’t make another stack outline for each function call, yet rather utilizes a solitary stack outline.ES6 brings the tail-call optimization in severe mode.

13. Classes in ES6

Previously there was no keyword class to create a class in ES5 for that it was a complex procedure to construct a class and use it. But in ES6 it is easier to work with the class. It uses prototypes, not the function factory method.

14. Inheritance

It is possible to inherit data of class to another class by inheritance in ES6. All instances of any particular class can be accessed by another class easily.

In this example, all properties of the Service class are inherited from SpecialService class.

15. Arrow Functions in ES6

Arrow function is one of the highlights presented in the ES6 form of JavaScript. It permits you to make capacities in a cleaner route contrasted with regular functions. They can be used to create small callbacks, with a shorter syntax. For instance,

16. Block-Scoped Constructs Let and Const

ES6 introduces two new variable declaration methods let and const. They are pretty similar but the difference is in the scope. Variables that declared by let can be changed or modified later but variables that declared with const are constant or fixed values that can not be changed.

17. Modules in ES6

Modules refer to a portion of code that is independent and can be used on multiple occasions. Before ES6 modules can only be used via libraries but now they can be used in the language itself. Before now, it was impossible to directly reference or include one JavaScript file in another.

18.Js Hoisting

Hoisting is JavaScript’s default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).

19.Block-Level Declarations

Block-level variable declaration writes into the {} and this variable can be accessed from these braces and not outside the braces.

--

--