How To Handle Try & Catch

Lets tryout try and catch with JavaScript:

Shapan Miah
4 min readMay 6, 2021
Photo by Claudio Schwarz | @purzlbaum on Unsplash

What is Error:

JavaScript mainly reads the code firstly and then run code and throw output. If code is valid and value is right then code run properly and output are all set. But code is not valid or have any wrong issue in the code then javascript throw error output.

Type of error:

EvalError, RangeError, ReferenceError, SyntaxError, TypeError, UriError, AggregateError, InternalError.

Some Word of try & catch:

IF code valid than try is normaly executed. But code is not valid then try execution is stopped. And than catch error, Errors can be coding errors made by the programmer, errors due to wrong input, and other unexpected things.

Try & Catch look like this code:

try {
speech (“hello bangladesh!”);
}
catch(err) {
err.message;
}

Error less try & catch:

try {
if(a == 100) throw “no error”;
}
catch(err) {
console.log(err) // no error;
}

Try & catch with error:

try {
if(a !== 100) throw “error”;
}
catch(err) {
console.log(err) // error;
}
}

How To Write Perfect And Relevant Comments In JavaScript:

Photo by Ferenc Almasi on Unsplash

What is Comments:

Comments are sometimes also process in various ways to produce documentation external to the source code itself by documentation generators, or used for integration with source code management systems and other kinds of external programming tools.

Comments Example:

var a = b+ 5; // Declare a, give it the value of b+ 5.

Bad Comments Example:

function megaFriend (friendNames) {    //let largeName = [0]; for (let i = 0; i < friendNames.length; i++) {
//let element = friendNames[i];
if (element.length > largeName.length) {
//largeName = element;
largeName = element;
}
} return largeName;}

Good Comments Example:

//
The code below will change the heading with date= “myH” and the paragraph with id = “myP” in my web page:
//

Explore Block Bindings:

Photo by La-Rel Easter on Unsplash

Var Declarations and Hoisting:

Variable declaration using (var), And this declaration var is globaly accessable if this declare is global scope or out of function, And this is call hosting.

var name = "habib khan"

Explore Block-Level Declarations:

Block-level declarations means that declare variables this is not accessible outside of block scope. Block scopes are created Inside of a function and Inside of a block.

Let Variable Declarations:

Let declaration is limit access in block scope. But let declare value is changeable in any time. Here code is below….

let userName = "sakib";
userName ="tamim";
console.log(userName) // tamim

Functions in Loops With let Variable Declaration:

function friends (friendNames) {
let bestFrend = [0];
for (let i = 0; i < friendNames.length; i++) {
let element = friendNames[i];
if (element.length > bestFrend.length) {
bestFrend= element;
}
}
return bestFrend;
}

Const Variable Declarations:

You can also define variables in ES6 with the const declaration syntax. Const declaration is limit access in block scope and this declare is not changeable for any time.

Const declaration:

const name = "jamal uddin";
console.log(name); // jamal uddin
const frieds = ["rahim", "karim", "jabbar"]
friends [1] = "faruk";
console.log(friends); // ["rahim", "faruk", "jabbar"]
const frieds = ["rahim", "karim", "jabbar"]
friends = [23, 45, 56];
console.log(friends); // error

Global Block Bindings:

Global block binding means there declare variable access in global scope, And it’s called global variable. This is the old system are published before the ES6.

Global Block Binding in Loops:

var applePrice = 0;
for(var i = 0; i < 100; i++){
applePrice = applePrice+ 1;
}
console.log(applePrice);

Functions with Default Parameter Values:

Default parameter is the useful thing if function parameter value is missing then default value is working and save the error.

Some Example For Default Parameter:

If parameter is provide completely then output is ok..

function calculate(number1, number2){
return number1 + number
}
const total = calculate(10, 15);
console.log(total); // 25

If any parameter are not provide or missing unfortunately then output is error..

function calculate(number1, number2){
return number1 + number
}
const total = calculate(10, );
console.log(total); // NaN

Than provide default parameter in function:

Option 1:

function calculate(number1, number2){
if(number2 == undefined){
number2 = 0;
}
return number1 + number
}
const total = calculate(10, );
console.log(total); // 10

Option 2:

function calculate(number1, number2){
number2 = number2 || 10;
return number1 + number
}
const total = calculate(10, );
console.log(total); // 20

Option 3:

function calculate(number1, number2 = 20){
return number1 + number
}
const total = calculate(10, );
console.log(total); // 30

Explore The Spread Operator:

The spread operator is a new addition to the set of operators in ES6. Spread operator takes in an iterable and expands it into individual elements. The spread operator is denoted by three dots, ….

Example of spread operator:

const bestFriends = ["Rohim", "Karim", "Shohag", "Tamim"];
const normalFriends = ["Kalam", "Sakib", "Himel", "Anik"];
const friendOfFriends = ["Jahid", "Shamim", "Akash"];
const allFriends = [...bestFriends, ...normalFriends, ...friendOfFriends];console.log(allFriends);
// allFriends = ["Rohim", "Karim", "Shohag", "Tamim", "Kalam", "Sakib", "Himel", "Anik", "Jahid", "Shamim", "Akash"];

--

--