Essential JS coding terms and how to use them

1. JavaScript try/catch/finally Statement

While writing codes it is very common that error happens and to handle these errors JS have 4 statements. These are try, catch, throw and finally.

Syntax:

Example:

2. JavaScript throw Statement

If we want to add custom error then the throw statement is required. Here is an example how can we use the throw statement in error handling.

3. JS Commenting

Sometimes its needed to add some comment in code to make it understandable to other coders. JavaScript allow codes to add comment in codes by two ways.
The first one is single line commenting and it starts with // sign and the comment continues till the end of the line. That means the execution does not occurred on the commented line.

Syntax:

Example:

The second way of commenting is multiline commenting and its needed when the coder wants to briefly write some lines about the code or some lines of codes which the coder does not want to execute.

Syntax:

Example:

Here the first block of codes will not be executed.

4. JavaScript Hoisting

One most significant default behavior of JavaScript is moving declarations to the top and it is called Hoisting. To avoid unexpected bugs all the variables should be at the beginning of every scope and it is the best practice.

To understand this topic lets see 2 examples. The result of the two examples will not be same because of the variables declaration.

Example:

5. Block-Level Declarations

The Block-Level Declarations can be made with two ways. These are:
1.Inside of a function
2.Inside of a code block

let Declarations
Syntax for the let declaration syntax is as like as the syntax for var. To declare a variable one can basically replace var with let to declare a variable, but the limit is only the current code block.

Example:

6. Block Binding in Loops

For Block Binding in Loops we have to use let not var, cause var is being hoisted.

Follow the two examples below:

As the var declaration gets hoisted the variable i is still accessible after the loop is completed. To ignore this problem we can use let instead, as like as the following code:

In this example, the variable i only exists within the for loop.

7. Functions with Default Parameter Values

To initialize functions with default values default parameters are used in ES6. A default parameter is used when an argument is either excluded or undefined. A default parameter can be anything from a number to another function.

8. The Spread Operator

In general, the Spread Operator doing the job of concatenation. Basically spread operator works as like as concat() method. It will give the output without changing the original value of the variable array where more than 1 values are expected.

For example:

9. Arrow Function

Arrow function is slightly different from a normal function and can’t be used in all situations.

With the comparison to traditional function it can be clearly understood.

For example in general function:

In arrow function:

10. JS Coding Styles

Naming convention

Variable name and function name with camelCase format is recommended in JS.

Example:

Space around operators

Space should be used around the operators and also after the commas.
Example:

Code indentation

2 spaces for indentation of code block.

Line length

If the line length of code is greater than 80 characters and it does not fit in one line, the best practice is to break the line in new line.

Example:

Empty line between logical blocks

Example:

References:

  1. https://www.w3schools.com/js/
  2. https://javascript.info/
  3. https://developer.mozilla.org/en-US/docs/Web/JavaScript
  4. http://imtiaz.cloud/block-binding-in-es6/

B.Sc. in Computer Science and Engineering. University of Barishal, Barishal.