Increment decrement operator in JavaScript

Source: Internet
Author: User

In JavaScript, incrementing decrements belong to the unary operator, the so-called unary operator, which is an operator that can manipulate only one value.

Increment and decrement operators are available in two versions: pre-and post-built. As the name implies, the predecessor should precede the variable to be manipulated, and the latter should precede the variable to be manipulated.

01, pre-type operator use:

var age=29; //Equals  - equivalent to var age=29;age//equals 

When you perform a pre-increment and decrement operation, the value of the variable is changed before the statement is evaluated.

var num1=2; var num2=20; var num3=--num1+num2;  // equal to var // equal to

02, post-type operator use:

var age=29;age//equals 

There is a very important difference between post increment, decrement and forward increment and decrement, that is, increment and decrement operations are performed after the statement that contains them is evaluated.

var num1=2; var num2=20; var num3=num1--+num2;  // equals var // equal to

All of these four operators apply to any value, that is, they apply not only to integers, but also to strings, Boolean values, floating-point values, and objects.

varS1= "2";varS2= "Z";varb=false;varf=1.1;varo={valueOf:function(){        return-1; }};S1++;//The value becomes the number 3s2++;//value becomes Nanb++;//The value becomes the number 1f--;//value becomes 0.10000000000000009 (due to floating-point rounding errors)o--;//value becomes numeric-2

Increment decrement operator in JavaScript

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.