Fourth chapter JavaScript operators

Source: Internet
Author: User

Table of Contents: One, self increment, decrement operator two, Boolean operator (!, &&, | |) Three, multiplicative operator (multiplication, division, modulus) four, addition five, the relationship Operation selector, self-increment, decrement operator:

++/--before, then the first operation and then follow-up work. After that, the work is followed up and then calculated. Like what:

<! DOCTYPE html><script>var a = 3; alert (a++);   3  First Alert and then self-increment. alert (a);     4</script></body>

Some features: When the operand is not a valid number, JavaScript automatically converts the type of the operand.

  • If it is a string and is made up of valid numeric characters, it is converted to a numeric value, and if it is not a valid numeric character, it is converted to Nan, and the empty string is converted to 0.
  • False to convert to 0,true to 1.
  • When the operand is of type object, call the ValueOf () method, and if it returns Nan continue to call the ToString () method, and then continue with the rule above.
    <script>varA =true;varb =false;varc = "18.3";varD = "18d2.4";varE =NewObject ();varf = ""; alert (++A);//2,true converted to 1 after self-incrementalert (++B);//1,false converted to 0 after self-incrementalert (++C);//19.3, string containing valid numeric characters, converted to numeric self-incrementalert (++D);//Nan, the string does not contain a valid numeric character and is converted to Nanalert (++e);//nan, call the valueof () method of E, and then return Nan, continue calling the ToString () method, or Nan, an invalid numeric stringalert (++F);//1, the empty string is converted to 0, and then the self-increment</script>
Boolean operator (!, &&, | |) :

!: Logical non-operator: it first converts the operand to a Boolean value, then takes the inverse, following the rules:

    • Operand is object: returns FALSE.
    • The operand is an empty string and returns True.
    • The operand is a non-empty string and returns False.
    • The operand is a numeric value of 0, which returns TRUE.
    • The operand is any non-0 numeric value (including infinity) and returns false.
    • The operand is null,nan,undefined and returns True.

!! : Two logical non-operations are equivalent to executing the Boolean () function.

&&: Logic and operation are short-circuiting operations, that is, if the first operand can determine the result, then the second operand is no longer evaluated. If the first operand is false, the result can no longer be true regardless of the value of the second operand.

Logic and operations can be used with any type of operand, not just a Boolean value, which follows the following rules:

    • Returns the second operand if the first operand is an object.
    • If the second operand is an object, the object is returned only if the first operand has a search result of true.
    • If the two operands are objects, the second operand is returned.
    • If one of the operands is NULL, then NULL is returned (except for the number of false operands).
    • If one of the operands is Nan, a nan is returned (except for the number of false operands).
    • If one of the operands is undefined, the undefined is returned (in addition to the false operand).
<script>varA =true;varb =false;varc = "18.3";varD = "18d2.4";varE =NewObject ();varf =NewObject ();varx=NULL; alert (b&& c);//false, because B is false, so it will never be true. Alert (a && e);//Object , because A is true, and the second operand is an object. Alert (e && b);//false, because E is the object, so the second operand is returned falseAlert (e && a);//true because E is the object, so the second operand is returned trueAlert (e && f);//Object , which returns the second F because it is both an object. Alert (a && x);//undefined, because x is undefined, and a is true, if a is false, the entire expression is false, not undefined. // ---------------------------Alert (a && (++c));  ++c arithmetic, the return result is 19.3, first A is true, then ++c is 19.3, but 19.3 becomes an object, so the Objectalert (c) is returned; 19.3, you can see that the ++c is running.  c = 0;alert (b && (++c));  ++c operation, the return result is false, because B is false, so ++c does not calculate the alert (c) at all; 0</script>

| |: Logical OR logical with the same as the short-circuit operator, if the first operation result is true, the second operand is not evaluated, and the following results are followed:

    • If the first operand is an object, the first operand is returned.
    • If the evaluation result of the first operand is false, the second operand is returned.
    • If the two operands are objects, the first operand is returned.
    • If the two operands are null, Nan, undefined, null, Nan, and undefined are returned.

Due to | | is a short-circuit operator, you can use this feature to provide a fallback operation for variable assignment. Like what:

var my = var1 | | VAR2, so that the var1 is assigned to my first, and if VAR1 does not have a valid value, VAR2 is assigned to my.

Three, multiplicative operator (multiplication, division, modulo):

If an operand is not a numeric type, JavaScript calls the number () function in the background to convert and then computes. True to 1,false to 0, empty string to 0.

<script>var a = ""; var b = 3; var c = NaN; var d = undefined; var true ; var false ; alert (a*b);     // 0, because the empty string is converted to 0alert (e*b);    // 3,true converted to 1alert (f*b);  // 0,true conversion to 0</script>
Iv. addition:
    • If one of the operands is a string, the following is the rule:
    • If two is a string, concatenate two strings.
    • If one is a string, the other operand is converted to a string and then concatenated.
    • If one is an object, a numeric value, or a Boolean, call their ToString () method, and then follow the rules above. Undefined and null only get their own characters, that is, "undefined", "null".
 <script>var  a= "abc"  var  b = 123;  var  C = undefined;  var  d = null           var  e = "123" ;alert (a  +b); //        abc123  alert (a+c); //     abcundefined  alert (a+d); //   abcnull  alert (b+e); // 123123  </script> 

That is, if the string type is to be added to the numeric type, it must be converted to a numeric type!

v. Relational operators:

Relational operators:

    • In the case of string comparisons, the character encoding value of each character in the corresponding position in the two string is compared, which is the sequential value in ASCII encoding instead of returning the comparison in alphabetical order.
    • Undefined and null are equal.
    • Returns true if all two operands are objects, or false if all points to the same object.
    • Nan and Nan are not equal.
    • If one operand is a number and the other is a string, the string is converted to a numeric value for comparison.
    • If the operand is an object, call the ValueOf () method, and if not, call the ToString () method, and then calculate in the order above.

<script>var a= "123"; var b = 123; var c = "456"; alert (a==b);  // True,a converted Equal alert (a<c);  // true, according to the alphabet "1" ==49, "4" ==52, so is the 4 big </script>

Fourth chapter JavaScript operators

Related Article

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.