Some important things should know a JavaScript developer

Habibullah Bahar Piash
3 min readMay 6, 2021

As I’m a JavaScript learner. I realize that these things will help you understand JavaScript very well. Also, these things will help you to play with JS codes comfortably

Error Handling(try….catch)

When we are writing codes in JavaScript, sometimes our scripts occur error because of our mistake or unexpected user input, or something else.

There is a nice way to handling errors in JavaScript, this is ‘try…catch’. It allows us to catch errors so that the script can do something instead of dying.

Syntex of try…catch:

try { 
// code…
} catch (err) {
// error handling
}

Coding Style

We should write and make our code as clean and easy to read as much as possible. This is the art of programming. Let’s talk about some suggested rules of coding style.

I. Curly Braces

Bad Practice:

if (5 < 7) { alert(“Hello, Piash”); }

Good Practice:

if (5 < 7) {     alert(“Hello, Piash”); }

II. Indents

There are two types of indents:

  1. Horizontal indents: 2 or 4 spaces.
  2. Vertical indents: empty lines for splitting code into logical blocks

Example:

function pow(x, n) { 
let result = 1;
// ←
for (let i = 0; i < n; i++) {
result *= x;
}
// ←
return result;
}

III. Semicolons

A semicolon should be present after each statement, even if it could possibly be skipped.

Some programming languages are required to give semicolons after a statement. But, there is no restriction in JavaScript. But, It is good practice to use semicolons after a statement.

You should learn more coding styles. So, you can visit https://javascript.info/coding-style for learning more.

Comment in code

Comment can make your code more readable to other developers.

There is two way of commenting in JavaScript. One is a single-line comment.

// Your comment

You also can write a multi-line comment:

/* 
your comment
write someting
*/ .

You can learn more of the good and bad practices of writing comments in JavaScript code from https://javascript.info/comments .

Features of ES6

  1. Template Literals in ES6

Template Literals is a way to create dynamic strings. You can use variables inside template literals.

var name = "Habibullah";
var age = 20;
var details = `My name is ${name} and my age is ${age}.`;
// It will give you the output
// My name is Habibullah and my age is 20.

2. Destructuring

Destructuring is a way to make code clean. 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.

var person =  {   
name : "piash",
age : 20,
profession: "Web Developer"
}
let {name,age,profession} = person;console.log(name); // "Piash"
console.log(age); // 20
console.log(profession); // "Web Developer"

3. 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.

class baseModel {  
constructor(options = {}, data = []) { // class constructor
this.name = 'Base'
this.url = 'http://azat.co/api'
this.data = data
this.options = options
}
getName() { // class method
console.log(`Class name: ${this.name}`)
}
}

--

--

Habibullah Bahar Piash
Habibullah Bahar Piash

Written by Habibullah Bahar Piash

Web Design & Development is my passion! I love to learn and explore new tech things.

No responses yet