JavaScript's self-amplification typical operation (error-prone)

Source: Internet
Author: User

JavaScript "self-increment" operators and expressions

The increment operator (+ +) is a unary operator that increments the operand by 1 at a time. The increment operator requires that its operand be a variable, a property in an object, or an element in an array, and that the type of the operand must be numeric.

If the operand type is not a numeric type, the increment operator converts the its first to numeric data and then increments the operation.

The increment operator (the self-increment operator) has two different increments depending on its position relative to the operand.

1. "Pre-increment" operator

Format:

++a

The pre-increment operator first adds 1 to the value of the operand and then participates in the operation of the expression using the new value of the operand.

Example:

var a=10, B;b=++a;

Results: a=11,b=11

2. "Post-increment" operator

Format:

a++

The post-increment operator first participates in the operation of the operand's value, after which the value of the expression is referenced, and then adds 1 to the value of the operand.

Example:

var a=10, B; b=a++;

Results: a=11,b=10

tip: when "+ +" is in front of a (++a), a plus 1 participates in the expression operation, and when "+ +" is behind A (a++), a first participates in the expression operation, and then adds 1.

"+ +" is only meaningful to a, you can think (++a) or (a++) as a whole, but a first plus 1 to participate in the operation, or to participate in the operation and add 1 of the problem.

  Case One
var a = 1;
var B = ++a + ++a;
Console.log (b); Result is 5

var a = 1;
var c = a++ + a++;
Console.log (c); Result is 3

var a = 1;
var d = a++ + ++a;
Console.log (d); Result is 4

var a = 1;
var e = ++a + a++;
Console.log (e); Result is 4

   

  Case Two
var a = 1;
var B = ++a + ++a; ==> first a=a+1 get a=2, then 2 + ++a, then a=a+1 get a=3, finally 2+3=5, at this time a value of 3
Console.log (b); Result is 5
var c = a++ + a++; The value of a after the ==> operation is 5
Console.log (c); Result is 7
var d = a++ + ++a; The value of a after the ==> operation is 7
Console.log (d); Result is 12
var e = ++a + a++; The value of a after the ==> operation is 9
Console.log (e); Result is 16

JavaScript "decrement" operator and expression

The decrement operator (the self-decrement operator) has two different ways of decreasing its position relative to the operand, that is, the pre -decrement and post -decrement.

The method of operation above, here is not more verbose.

JavaScript's self-amplification typical operation (error-prone)

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.