Home > Common Problem > What is the difference between x-- and --x

What is the difference between x-- and --x

coldplay.xixi
Release: 2020-11-30 11:44:08
Original
39872 people have browsed it

The difference between x-- and --x: 1. [--x] decrements the value of x by 1 first, and then calculates the value of x; 2. [x--] calculates x first value, and then decrement the value of x by 1.

What is the difference between x-- and --x

##The difference between x-- and --x:

  • - -x means that the value of x is decremented by 1 first, and then the value of x is calculated.

  • x-- is to first calculate the value of x, and then decrement the value of x by 1.

Example 1:

var x = 10;
console.log(x--);
console.log(x);
Copy after login

Output:

10
9
Copy after login

The first output is 10, x--first use the value of x in the current expression , and then decrement the value of x by 1; the second output is 9, because x has decremented by 1 after the previous instruction.

Example 2:

var x = 10;
console.log(--x);
console.log(x);
Copy after login

Output:

9
9
Copy after login

The first output is 9, --x first decrements the value of x by 1, and then in the current expression Use the value of x; the second one also outputs 9, and x has been decremented by 1 after the previous instruction.

The above is the detailed content of What is the difference between x-- and --x. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template