Javascript:if can be used as a condition of judgment

Source: Internet
Author: User

if is one of the longest judgments in all programming languages, what exactly can be used in the if Chinese as a judgment expression in JS?

For example, how many lines, just a few parentheses, true and false is completely different, what does it mean?

var obj={};
 Obj. Funtext=function () {};
    if (obj. Funtext)
   {
     alert ("True  obj.") Funtext; ");
   }
   else
   {
     alert (' False  obj '. Funtext ");
   }
   Obj. Funtext=function () {};
    if (obj. Funtext ())
   {
     alert ("True  obj.") Funtext (); ");
   }
   else
   {
     alert (' False  obj '. Funtext () ");
   }

1 First class defined variable but not assigned value in if is considered false

For example:

          var  t;
          if (t)
          {
            alert ("True has undefined value");
          }
          else
          {
            alert ("False defined Unassigned");
          }

2 The second class of defined variables, the assignment is an empty string that is considered false in if, assigned to another string, that is, the character in the string is considered true

For example:

  var  t;
  T= "";
   if (t)
   {
     alert (' True t= '; ');
   }
   else
   {
     alert ("False t= '");
   }

If the judgment is false

again for example:

         var  t;
          T= "";
          if (t)
          {
            alert (' True t= '; ');
          }
          else
          {
            alert ("False t= '");
          }
           T= "the";
          if (t)
          {
            alert ("True t= ';");
          }
          else
          {
            alert ("False t= '");
          }

If judgment is true, that is, for string types, as long as there is a character, even a space character if the judge is true.

3 Third class defined variable, the assignment is true in if it is true and the assignment is false, false, which is the same as a variable of type bool in other languages.

For example:

var  t;
 T=false;
 if (t)
 {
   alert ("True  t=false;");
 else
 {
   alert ("False  t=false;");
  T=true;
 if (t)
 {
   alert ("True  t=true;");
 else
 {
   alert ("False  t=true;");
 
4 class Fourth defined variable, assignment 0 is false in if, other values are true, this is the same as the variable of the type of numeric value in C language.

For example:

var  t;
 t=0;
 if (t)
 {
   alert ("True  t=0;");
 else
 {
   alert ("False  t=0;");
  t=0.0;
 if (t)
 {
   alert ("True  t=0.0;");
 else
 {
   alert ("False  t=0.0;");
 

The test found that either 0 or 0.0 were fake.
var  t;
  t=2;
 if (t)
 {
   alert ("True  t=2;");
 else
 {
   alert ("False  t=2;");
 

I found out that 0 is true. 5 The special value of the fifth class JS null,undefined, it's all fake .
         var  t=null;
           if (t)
           {
             alert ("True  t=null;");
           else
           {
             alert ("False  t=null;");
            t=undefined;
           if (t)
           {
             alert ("True  t=undefined;");
           else
           {
             alert ("False  t=undefined;");
           

Because the default value of a variable not defined in JS is undefined, this explains the first kind of situation



6 The defined functions of class sixth are divided into two types according to the method of invocation

The first: without parentheses, if the definition is true, there is no definition that will give an error

          function TestFunction () {}
           if (testfunction)
           {
             alert ("True  testfunction;");
           else
           {
             alert ("False  testfunction;");
           

The second: parenthesized, in fact, is equivalent to call function, naturally, based on the return value of the function to determine true and false

For example:

    function TestFunction () {}
    if (TestFunction ())
          {
            alert ("True  testfunction;");
          else
          {
            alert ("False  testfunction;");
          

is false because, if the function does not define a return value, the return value is undefined



7 class seventh defined object that is false when unassigned, and true after assignment.

For example:

           var obj;
            if (obj)
           {
             alert ("True  obj;");
           }
           else
           {
             alert ("False  obj;");
           }

In fact, because the variable in JS is not assigned to the type, so and the first case is the same.

But when you assign a value, it becomes true, for example:

         var  obj={};
            if (obj)
           {
             alert ("True  obj={};");
           else
           {
             alert ("False  obj={};");
           

8 class eighth a defined object's property field is the same as a separate variable, for example, a value of 0 o'clock is false, others are true, the string is false for null values, others are true.

For example

          var obj={};
          Obj. Text= "";
           if (obj. Text
          } {
            alert ("True  obj.") Text; ");
          else
          {
            alert (' False  obj '. Text ");
          }
           Obj. text= "Text";
           if (obj. Text
          } {
            alert ("True  obj.") Text; ");
          else
          {
            alert (' False  obj '. Text ");
          }
          Obj. text=0;
           if (obj. Text
          } {
            alert ("True  obj.") Text; ");
          else
          {
            alert (' False  obj '. Text ");
          }
           Obj. Text=1;
           if (obj. Text
          } {
            alert ("True  obj.") Text; ");
          else
          {
            alert (' False  obj '. Text ");
          }

9 The methods of the Nineth class of defined objects are the same as individual functions,

No parenthesis is if not defined is false,

           var obj={};
            Obj. Funtext=function () {};
            if (obj. Funtext)
           {
             alert ("True  obj.") Funtext; ");
           }
           else
           {
             alert (' False  obj '. Funtext ");
           }         
            if (obj. FUNTEXT1)//undefined property, and no method
           {
             alert (' True obj ') is defined  . Funtext1; ");
           }
           else
           {
             alert (' False  obj '. Funtext1 ");
           }
Adding parentheses is equivalent to calling the method, which is based on the return value to determine true or false.
            var obj={};
             Obj. Funtext=function () {};
            if (obj. Funtext ())
           {
             alert ("True  obj.") Funtext (); ");
           }
           else
           {
             alert (' False  obj '. Funtext () ");
           }
            Obj. Funtext2=function () {return "FF"};
            if (obj. FUNTEXT2 ())
           {
             alert ("True  obj.") Funtext2 (); ");
           }
           else
           {
             alert (' False  obj '. Funtext2 () ");
           }

You can see that there are a lot of types that can be judged in the IF in JS, but ultimately they can be seen as variants of these types. As long as you master these most basic, you can flexibly use if judgment.

the most basic is null,undefined,if judgment is false; for numeric types, 0 is false, others are true; empty strings for character types are false, others are true, for method attributes, if the definition is true, otherwise it is false, and all else can be seen as a disguised application.





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.