Let’s take a look at some improvements in Javascript

Noor Hossain
3 min readMay 7, 2021

Error handling, “try…catch” in javascript

We often go through a lot of errors when programing code.Then we need to debug.And then we call the try…catch So that we can catch blocks.It will first execute all the codes in our block, if the effective is successfull, the catch will not be effective, if not succeeed then the catch will be effective.

Error object in Javascript

When the catch is caught by any error javascript generate an object containing the details about it.The object is passed as the argument after getting caught

try {console.log( obj )}
catch (error){
console.log (error.name) // ReferencError
console.log (error.message) // obj is not defined}

Rethrowing in javascript

In the example, we try to catch the wrong data to handle, but it is possible? if it is possible to crate an unexpected way to create an error. Like a programming error.

let json = '{ "age": 30 }'; // incomplete data  try {   user = JSON.parse(json); // <-- forgot to put "let" before user  
// ...
} catch (err) { alert("JSON Error: " + err); // JSON Error: ReferenceError: user is not defined

// (no JSON Error actually)
}

try…catch…finally in Javascript

In addition to the names of two blocks mentioned above, there is a block name, which is finally block.This block is always making the block.

try { 

... try to execute the code ...
} catch (err) { ... handle errors ... } finally {

... execute always ...
}

Coding Style with Curly bracs

In most JavaScript projects curly braces are written in “Egyptian” style with the opening brace on the same line as the corresponding keyword — not on a new line. There should also be a space before the opening bracket, like this:

if (condition) {   
// do this
// ...and that
// ...and that
}

Coding Style with line length

Nobody likes to read the long line of code, droping the beauty of code.It’s best practice to separate them.

// backtick quotes ` allow to split the string into multiple lines let str = `   
ECMA International's TC39 is a group of JavaScript developers, implementers, academics, and more, collaborating with the community to maintain and evolve the definition of JavaScript.
`;

Coding Style with Semicolons

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

There are languages where a semicolon is truly optional and it is rarely used. In JavaScript, though, there are cases where a line break is not interpreted as a semicolon, leaving the code vulnerable to errors. See more about that in the chapter

when variable is declared with var

If we do variables var declare outside of a function, it can be lift in global opportunities, So it is accessible from any place.

when variable is declared with let

If we do variables var declare outside of a function, it can not be lift in global opportunities, So it is not accessible from any place.

When to use comments in code

When you think that your code is not readable, it is not correct for writting comments about how the code will work, then the another code writting and comments of the previous code and then run.its a use comments.

--

--