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

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

coldplay.xixi
coldplay.xixiOriginal
2020-11-11 14:19:5639943browse

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);

Output:

10
9

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);

Output:

9
9

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!

Statement:
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
Previous article:What is wsl?Next article:What is wsl?