Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 21, 2022 02:51 pm GMT

Some important things should know a JavaScript developer

As Im 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 trycatch. It allows us to catch errors so that the script can do something instead of dying.

Syntex of trycatch:

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. Lets 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); // 20console.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}`)        }}

My Portfolio Website: https://habibullahftl.web.app/


Original Link: https://dev.to/habibullahftl/some-important-things-should-know-a-javascript-developer-1g5e

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To