power of x ^ y
//Short syntax of power of
console.log(2 ** 5); //32
//using Math operation
console.log(Math.pow(2, 5)); //32
for of usage
//2.for of usage
let productDetails = [
{
product_Id: 1,
product_Name: "LG Monitor",
price: "55k",
rating: "super",
},
{
product_Id: 2,
product_Name: "LG Curve Monitor",
price: "45k",
rating: "Average",
},
];
for (let obj of productDetails) {
console.log(JSON.stringify(obj));
}
/*
Basically used to send data to backend server
{"product_Id":1,"product_Name":"LG Monitor","price":"55k","rating":"super"}
{"product_Id":2,"product_Name":"LG Curve Monitor","price":"45k","rating":"Average"}
*/
+ to convert string to number
// + sign to convert string to number if possible
console.log(200 + "200"); //200200
console.log(200 + +"200"); //400
console.log(200 + +"sda"); //NaN
use strict
//4.Use Strict
/*
1.cant use a variable without let var or const keyword
2.cant use reserve words as variables
let eval =10
3.cant delete a variable
4.cant delete a function
Best practice always use - 'use strict'
*/
function useStrict() {
"use strict";
result = 10; //this cant be done - rule 1
let eval = 15; //this cant be done - rule 2
delete result; //this cant be done - rule 3
delete useStrict; //this cant be done - rule 4
}
Guess the output
/*
error has properties
1.message -->error message
2.name --> tells you which error name it is ...
*/
function tryCatch() {
let result;
try {
result = y / 100;
} catch (error) {
console.log(error.message); //y is not defined
console.log(error.name); //ReferenceError
}
}
tryCatch();
Throw your own custom error object
function tryCatch1() {
let result;
try {
result = y / 100;
} catch (error) {
throw {
message: "tryCatch() method while division has given this error" + error.message,
name: "Its a custom error",
};
}
}
tryCatch1();
//error in console ->
// Uncaught
// {message: 'tryCatch() method while division has given this errory is not defined', name: 'Its a custom error'}
// message: "tryCatch() method while division has given this errory is not defined"
// name: "Its a custom error"
// [[Prototype]]: Object
Handle All errors
//Good Practice to pull out a method and handle all types of error
function HandleAllError(error) {
switch (error.name) {
case "ReferenceError":
console.log("1.Reference Error::-" + error.message);
break;
case "RangeError":
console.log("2.Range Error::-" + error.message);
break;
case "TypeError":
console.log("3.Type Error::-" + error.message);
break;
case "URIError":
console.log("4.URI Error::-" + error.message);
break;
case "SyntaxError":
console.log("5.Syntax Error::-" + error.message);
break;
case "EvalError":
console.log("6.EvalError::-" + error.message);
break;
default:
console.log("Error type is :" + error.name + " --> Error Message ::- " + error.message);
}
}
//1.referenceError
const referenceError = () => {
let finalResult = 0;
try {
//y is not defined
finalResult = y * finalResult;
} catch (error) {
HandleAllError(error);
}
};
//2.RangeError
const rangeError = () => {
let diceThrow = 7;
try {
if (diceThrow <= 1 || diceThrow >= 6)
throw new RangeError("Dice throw should be between 1 and 6");
} catch (error) {
HandleAllError(error);
}
};
//3.typeError
const typeError = () => {
try {
let num = 20;
//subString cant be done on number ..
num.subString(1);
} catch (error) {
HandleAllError(error);
}
};
//4.URIError
const uriError = () => {
let uri = "http://%%%%%ldksjfljsdf ";
try {
decodeURI(uri);
} catch (error) {
HandleAllError(error);
}
};
//5.syntaxError
const syntaxError = () => {
try {
//single quote is missing
let confirmation = eval("confirm(' Are you sure you want to proceed)");
} catch (error) {
HandleAllError(error);
}
};
//6.EvalError
//use of eval should be avoided as much as possible..
const evalError = () => {
try {
throw new EvalError("eval error");
} catch (error) {
HandleAllError(error);
}
};
referenceError();
rangeError();
typeError();
uriError();
syntaxError();
evalError();