JavaScript operators: Increment and decrement (the difference between ++i,--i and i++,i--)

Source: Internet
Author: User

The increment and decrement operators draw directly from C, and each has two versions: pre-type (increment ++i , decrement --i ) and post-type (increment i++ , decrement i-- ). The definition of both in the book is that the predecessor should precede the variable to be manipulated, and the latter should precede the variable to be manipulated. What? Why is it so difficult to understand, in a more understandable language to describe how it should be ..., combined with the provision of code, a supercilious look .....

Pre-type (increment ++i , decrement --i ): is not the first self-calculation, and then assign to the variable (the first calculation of their own body to increase, reduce the amount of money, and then save it to their own bank card or to creditors);

Post-type (increment i++ , decrement i-- ): is not the first to assign their own value to the variable, and then self-calculation (first put their own money into their own bank card or to creditors, and then calculate on the original base on their own increase, reduce the amount of money) ;

Do you understand me? Still don't understand? Now let's start to show you how your money is circulating in two ways, ha:

first, pre- set ( increment + i, + 递减--i )

Suppose 你有100块(变量i=100) , then the predecessor increments to ++i .

var i =;alert (i); => 100++i;   =>      101    Calculate how much  is added to your body first     Alert (i)   ;   = 101    

It also corresponds to the following effect:

var i = + ;
i=i+1; = i=100+1=101;

When you perform a pre-increment and decrement operation, the value of the variable is changed before the statement is evaluated. (In the field of computer science, this is often referred to as a secondary effect )
, see the following example:

var i =;
alert (i);=
i++;Save the money you originally had on your own bank card or return it to your creditors .
alert (i);= = 101 Then in the calculation on the original basis of their own increase, reduced after a total of how much money

Placing the increment operator behind a variable does not change the result of the statement, because incrementing is the only action for this statement. But when the statement contains other actions, the difference is different. Look at the following example:
var num1=2;
var num2=20;
var num3=num1++ + num2;
var num4=num1+num2;
alert (NUM1);=3
alert (NUM3); < Span class= "hljs-comment" > < Span style= "FONT-SIZE:14PX; Font-family:microsoft Yahei "> => +//var i; i=num1=2; num1=num+1=3;num3=i + num2;
alert (NUM4);             < Span style= "FONT-SIZE:14PX; Font-family:microsoft Yahei "> < Span class= "hljs-built_in" > => //num1+num2;

In conclusion, I hope this article will help you understand the increment and decrement (the difference between ++i,--i and i++,i--), share the technology, share the happiness!

JavaScript operators: Increment and decrement (the difference between ++i,--i and i++,i--)

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.