Functional programming in JavaScript

Hi all,
anyone can explain me why when using incrementor in the code below I still get the fixed value, but when using aditional operator it increases the variable:

let fixedValue = 4;

function incrementer() {
  return fixedValue++;
}

//output: 4;
let fixedValue = 4;

function incrementer() {
  return fixedValue + 1;
}

//output: 5;

It is because fixedValue++ and ++fixedValue are two different operations. The first like you done are reading the value of the variable then incrementing, so if you console.log the incrementer function you will get the read value. The second you are incrementing then reading the value, this is the one you want to use to fix your code

1 Like