How To Handle Try & Catch

Lets tryout try and catch with JavaScript:

Photo by Claudio Schwarz | @purzlbaum on Unsplash

What is Error:

Type of error:

Some Word of try & catch:

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 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:

var name = "habib khan"

Explore Block-Level Declarations:

Let Variable Declarations:

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:

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 in Loops:

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

Functions with Default Parameter Values:

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:

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"];

Web Developer