General Problem in javascript and it’s solution

ARJU arju
2 min readMay 7, 2021

Bad coding is always bring anxicity

Most of the beginner do the same mistake

For example :

1 . Curly braces are not needed:

if (n < 0) {alert(`Power ${n} is not supported`);}

2 . Split to a separate line without braces. Never do that, easy to make an error wheb adding new lines:

if (n < 0)

alert(`Power ${n} is not supported`)

3 . One line without braces — acceptable, If it’s short

if (n < 0) alert(`Power ${n} is not supported`);

4. The best best best variant :

if (n < 0) {

alert(`Power ${n} is not supported`);

}

Try catch :

The try statement allows you to define a block of code to be tested for errors while it is being executed.

The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

The try and catch keywords come in pairs:

Syntax >>

try {

// Block of code to try}catch(Exception e) {

// Block of code to handle errors}

Block Statement :

The block statement is often called compound statement in other languages. It allows you to use multiple statements where JavaScript expects only one statement. Combining statements into blocks is a common practice in JavaScript. The opposite behavior is possible using an empty statement, where you provide no statement, although one is required.

Blocks are commonly used in association with if…else and for statements.

Block Statement

{

StatementList}

Labelled Block Statement

LabelIdentifier: {

StatementList}

Example :

var x = 1;

let y = 1;

if (true) {

var x = 2;

let y = 2;

}

console.log(x);

// expected output: 2

console.log(y);

// expected output: 1

Emerging Best Practices for Block Bindings

While ECMAScript 6 was in development, there was widespread belief you should use let by default instead of var for variable declarations. For many JavaScript developers, let behaves exactly the way they thought var should have behaved, and so the direct replacement makes logical sense. In this case, you would use const for variables that needed modification protection.

--

--