Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the function return when throwing an error - Javascript

I was reading the book Professional Javascript For Web Developers, and saw the following code. I have some questions about it:

  1. What does "throw new Error()" return? Undefined?
  2. What will happen to the code block of "if" if there is an error thrown out?

function matchesSelector(element, selector){

  if(element.matchesSelector){
      return element.matchesSelector(selector);
  }else if(element.msMatchesSelector){
      return element.msMatchesSelector(selector);
  }else if(element.mozMatchesSelector){
      return element.mozMatchesSelector(selector);
  }else if(element.webkitMatchesSelector){
      return element.webkitMatchesSelector(selector);
  }else{
    throw new Error("Not supported!");
  }
}


if(matchesSelector(document.body, "body.page1")){
  //do somthing
}
like image 227
Xinxin He Avatar asked Feb 21 '17 21:02

Xinxin He


1 Answers

When an error is thrown, if it is not caught using a try...catch block, the scope execution just stops.

Nothing is returned by that function, and if that function's return value is used somewhere in if statement, that if statement block is not executed as well.

like image 92
Luka Kvavilashvili Avatar answered Nov 04 '22 00:11

Luka Kvavilashvili