If else statement inside try catch Javascript
You can throw a new error to go directly to catch
like:
var errLogs = [];
try {
var a = "ABCD"; // or, test it with number 123
if (typeof a === "string") {
console.log("equel");
} else {
throw new TypeError("not equel")
}
} catch (e) {
console.log(e);
errLogs.push(e);
}
DEMO:
var errLogs = [];
function testString(value) {
try {
if (typeof value === "string") {
console.log("equel");
} else {
throw new TypeError("not equel")
}
} catch (e) {
console.log(e.message);
errLogs.push(e.message);
}
}
testString('ABCD');
console.log('Now testing with number --->')
testString(123);