Use Try-catch to determine whether a variable is declared undeclared or unassigned

Source: Internet
Author: User
This article mainly introduces the use of Try-catch to determine whether the variable is declared unassigned or not declared, need friends can refer to the purpose is if a variable is declared unassigned, you can directly assign a value, and can not change the scope of the variable     if not stated, it is declared again,     Search on the Internet, the common method is if (typeof (a) = = ' undefined ') {var a= ' ss ';},    But this method returns true for a variable that is not declared or declared to be unassigned. And if so,:    code is as follows: Var a;  function f () {  if (typeof (a) = = ' undefined ')   {var a=1;}  }  F ();  Console.log (a);    will display undefined because the F () only declares a local variable with the same name.     But if an unassigned variable is declared: if (novaluev==null), it returns true;    undeclared variable: if (nodeclarev==null), an error is made.     So you can do this:    code is as follows: function f () {  if (typeof (v) = = ' undefined ') {  try{  if (v==null)// Description V is declared unassigned   v=1; If V is a global variable, this does not change its scope  }  catch (Err) {//description V is not declared   var v;v=2; } }  Console.log (v);  }  F ();    This is also wrong because JS has a ' Declaration of Advance ' feature, that is, variables declared within the function are visible in the function and in the function's child function, regardless of where it is declared in the function.     So because of the Var v above, it causes either of the cases to go only try.     Modify:    code as follows: function f () {  if (typeof (v) = = ' undEfined ') {  try{  if (v==null)/= V is declared unassigned   v=1;//If V is a global variable, this does not change its scope  }  catch (Err) {// Description V is not declared   eval (' Var v '); v=2; It's not like this.  } }  Console.log (v); }  F ();    This is OK.     Write a judgment function that returns ' Nodeclare ' indicating that the variable is not declared, ' Novalue ' indicates that the variable has been declared unassigned, ' HasValue ' indicates that the variable has been declared assigned:    code as follows: function f ( V) {  if (typeof (v) = = ' undefined ') {  try{  if (v==null)   return ' Novalue '; }  catch (err) {& nbsp Return ' Nodeclare '; } }  else return ' HasValue '; }  var a;  console.log (F (a));  a=0 ;  Console.log (f (a));  Console.log (f (b));    again wrong ... console.log (f (b)); Error ......   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.