Error handling for Undefined and Undeclared in javascript

Undefined: It occurs when a variable has been declared but has not been assigned any value.

var technology; // not assign any value

console.log(technology)  //undefined

Using of typeof operator in javascript the error handling for undefined variation can handle easily.

Error handling:-

function myFunc(val) {
  if ( typeof val === "undefined") {
  console.log("val isn't initialized");
 }else{
    console.log(val)
  }

}
var myVar;
myFunc(myVar); //val isn't initialized

Undeclared: It means that the variable does not exist in the script at all or not declare using var, let or const keywords. The scope of the undeclared variables is always global. This will give runtime error with the return value as “undefined”. To verifies that no undeclared variable is present in our code we can use 'use strict' keyword in the javascript. This type of error will block the execution of the remaining part of the code with an error statement in the browser console

'use strict'; 
console.log(myVar) //ReferenceError: myVar is not defined

The try...catch statement can be used to handle such type of error and bypass the other part of the code to execute easily. Although the error statement will run still it will not block the other javascript code.

Error handling:-

try{
    myFunc(ss);
}catch(e){
    console.log(e) //ReferenceError: ss is not defined
}