First decrement in Javascript with post-decrement operator ("number-") differs from "number - 1"

Asked

Viewed 70 times

2

It is known that in javascript numero-- subtracts 1 from the value of numero and amounts to: numero = numero-1;

So why is there difference in the result in the execution of the two forms?

Form 1:

var numero = 10;
console.log (numero);
var diminuirNumero = numero--;
console.log(diminuirNumero);

Form 2:

var outroNumero = 10;
console.log (outroNumero);
var diminuirOutroNumero = outroNumero-1;
console.log(diminuirOutroNumero);

Running

var numero = 10;
console.log (numero);
var diminuirNumero = numero--;
console.log(diminuirNumero);

var outroNumero = 10;
console.log (outroNumero);
var diminuirOutroNumero = outroNumero-1;
console.log(diminuirOutroNumero);

  • The 3 answers are identical, I will mark as accepted the oldest answer ;-)

3 answers

4


This is because numero-- returns the value of numero before to decrement it:

var numero = 10;
console.log(numero); // 10

// primeiro retorna o valor atual (10) e depois decrementa
console.log(numero--); // 10

// depois de decrementado
console.log(numero); // 9


If you want to return the value after the decrease, just use --numero:

var numero = 10;
console.log(numero); // 10
var diminuirNumero = --numero;
console.log(diminuirNumero); // 9

For thus the number is first decreased, and then the result is returned:

var numero = 10;
console.log(numero); // 10

// primeiro decrementa e depois retorna o valor (9)
console.log(--numero); // 9

// depois de decrementado
console.log(numero); // 9

More details on documentation and in the language specification <- in fact, this link has a detailed description of this behavior:

Postfix Decrement Operator

  • UpdateExpression:LeftHandSideExpression--
    • Let lhs be the result of evaluating LeftHandSideExpression.
    • Let oldValue be ToNumeric(GetValue(lhs)).
    • Let newValue be Type(oldValue)::subtract(oldValue, Type(oldValue)::unit).
    • Perform PutValue(lhs, newValue).
    • Return oldValue.

Prefix Decrement Operator

  • UpdateExpression:--UnaryExpression
    • Let expr be the result of evaluating UnaryExpression.
    • Let oldValue be ToNumeric(GetValue(expr)).
    • Let newValue be Type(oldValue)::subtract(oldValue, Type(oldValue)::unit).
    • Perform PutValue(expr, newValue).
    • Return newValue.

That is, the postfix Operator (numero--) returns the value the number had before the decrease, already the prefix Operator (--numero) returns the value that the number has after the decrement is done. But both update the value of numero.


Finally, if you do not want to change the value of numero, then you do numero - 1. And in this case it works because first the account is made numero - 1, and then the result is assigned to diminuirNumero.

1

According to the documentation, when used as suffix, the decrement operator returns the value before of the operation and, when used as prefix, returns the value afterward to decrease.

Decrement

let x = 3;
const y = x--;

console.log(`x:${x}, y:${y}`);
// expected output: "x:2, y:3"

let a = 3;
const b = --a;

console.log(`a:${a}, b:${b}`);
// expected output: "a:2, b:2"

If used postfix, with Operator after operand (for example, x--), the decrement Operator decrements and Returns the value before decrementing.

If used prefix, with Operator before operand (for example, --x), the decrement Operator decrements and Returns the value after decrementing.

In free translation:

Decreasing

let x = 3;
const y = x--;

console.log(`x:${x}, y:${y}`);
// saída esperada: "x:2, y:3"

let a = 3;
const b = --a;

console.log(`a:${a}, b:${b}`);
// saída esperada: "a:2, b:2"

If used as suffix, with operator after operand (for example, x--), the decrease operator decreases and returns the value before to decrease.

If used as prefix, with operator before operand (for example, --x), the decrease operator decreases and returns the value after decreasing.

0

This happens because there is a difference in using the operator -- (or ++) and the other way (x + 1 or x - 1).

The difference is this: how much do you use the ++ (or --), the order in which it appears makes a difference: when it appears before the variable, the calculation is executed and the result is returned.

When the operator is placed after the variable, the result is returned before.

When you use: valor + 1 (or valor - 1) , the calculation is done before the assignment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.