Search code examples
javascriptarraysdecrement

Why does Array.length- - not subtract 1?


When I run: var a = ["a", "b", "c"] console.log(a.length--) it prints 3, but when I run: var a = ["a", "b", "c"] console.log(a.length-1) it prints 2. Why? Isn't decrementing the same as subtracting 1? Thanks in advance.


Solution

  • The decrement operator (--) decrements (subtracts one from) its operand and returns a value.

    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.

    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"