JavaScript expressions and unary operators

Source: Internet
Author: User

First, expression:
An expression is a "phrase" in ECMAScript, and the interpreter converts it to a value by calculation. The simplest expression is the literal or variable name. For example:

 5.96 //  numeric literal  ' Lee ' //  true  //  Boolean literal  null  //  null literal /java///  regular expression literal  {x:1, y:2} //  [
     
      //
       array literal, array expression  function  (n) {return  x+y;} //  box //  variable  

Of course, you can also create complex expressions by merging simple expressions. Like what:

// An expression of an addition operation typeof // to view an expression of a data type // Logical Operation Expressions

  

Two or one-tuple operator:

An operator that can manipulate only one value is called a unary operator.

1, increment + + and decrement--

The difference between front and rear:

In the absence of an assignment operation, the front and back are the same.

In an assignment operation, if the increment or decrement operator is pre-placed, then the predecessor operator accumulates or decrements the assignment first, and if it is the post operator, the value is then incremented or subtracted.

var box = +; // forward increment, add box 1, equivalent to box = box+1 // decrease the front, reduce box by a 1, equivalent to box = Box-1 // post-increment, ibid. // post-decrement, ibid.
var box = +; var // an age value of 101,box is added to 101 and then assigned to age. var //  Box is assigned to height before it is self-increasing.
     var box = +;       var height =--box      alert (height); // The result is 99, which is first self-subtraction and then assigned to height.        var box = +;       var age = box--;      alert (age); // The result is that 100,box is first copied to age and then self-reducing.

2. Other types of rules that apply unary operators

     varbox = ' 89 '; box++;//90, numeric strings are automatically converted to numeric values    varbox = ' AB '; box++;//nan, String contains non-numeric to Nan    varbox =false; ++box;//The 1,false turns into a value of 0, and the summation is 1.    varbox = 2.3; ++box;//3.3, add 1 directly    varbox = {//2, no toString or valueOf is set to NaNTostring:function() {return1; }  }; Box++;
var box = {};alert (box+ +); // NaN

3, plus and minus operators:

The add operation rules are as follows:

      varbox = 100; +box;//100, no effect on the value      varbox = ' 89 '; +box;//89, numeric string converted to numeric value      varbox = ' AB '; +box;//nan, String contains non-numeric to Nan      varbox =false; +box;//0, the Boolean value is converted to the corresponding value      varbox = 2.3; +box;//2.3, no change      varbox = {//1, do not set toString or valueOf is NaNTostring:function() {return1; }    }; +box;

The following are the rules for reducing operations:

varbox = 100; -box;//-100, for the value, the direct change negative      varbox = ' 89 '; -box;//-89, numeric string converted to numeric value      varbox = ' AB '; -box;//nan, String contains non-numeric to Nan      varbox =false; -box;//0, the Boolean value is converted to the corresponding value      varbox = 2.3; -box;//-2.3, no change      varbox = {//-1, does not set toString or valueOf is NaNTostring:function() {return1; }    }; -box;

The addition and subtraction operators are generally used for arithmetic operations, as well as for type conversions above.

JavaScript expressions and unary 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.