I still do not understand while in this particular case years++ causes the loop to be executed only once.

because && years++ translates to && 0 which will translates to falsey value.

If you want to use years++, initialize years to 1

function calculateYears(investment, interestRate, tax, desiredProfit) {
    var years = 1;
    while(investment < desiredProfit && years++){
      investment += (investment * interestRate) * (1 - tax);
    }

    return years - 1;
}
console.log(calculateYears(1000, 0.05, 0.18, 1100));

Answer from gurvinder372 on Stack Overflow
Discussions

while loop increment in Javascript - Stack Overflow
Main difference is you are incrementing ... one while in second one you check and then increment x. ... I totally get it. Thanks so much ! ... So your question is what's the difference between... ... The main difference is that in the first example, the x gets the 2 added on and is then compared to the value of n, whereas in the second example, x is compared to n before the 2 has been added and will therefore most likely run 1 more loop than you would ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
increment variable in while loop
Darcy Phillips is having issues with: Hi, I am being asked to construct a while loop that iterates over the elements of the "numbers" array, and increments the "counter" variable wit... More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
1
May 4, 2016
javascript - Can a for loop increment/decrement by more than one? - Stack Overflow
Are there other ways to increment a for loop in Javascript besides i++ and ++i? For example, I want to increment by 3 instead of one. for (var i = 0; i More on stackoverflow.com
๐ŸŒ stackoverflow.com
increment in for loop javascript - Stack Overflow
I don't know why but I can't increment value in for loop normally. for (var i = 0; i More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Launch School
launchschool.com โ€บ books โ€บ javascript โ€บ read โ€บ loops_iterating
Loops in JavaScript - performing repeated operations on a data set
When JavaScript encounters this ... true at the beginning of the while statement and the engine runs the loop's block. Inside the block, we output counter's value, then increment it by 1....
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Guide โ€บ Loops_and_iteration
Loops and iteration - JavaScript - MDN Web Docs - Mozilla
The following while loop iterates as long as n is less than 3: ... With each iteration, the loop increments n and adds that value to x.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ javascript-while-loop
JavaScript while Loop
March 25, 2025 - If the condition results true, the number will add to the total. Otherwise, it will exit from the JavaScript iteration. After adding the value to the total variable, we used the ++ operator to increment the number value.
๐ŸŒ
DEV Community
dev.to โ€บ bhagatparwinder โ€บ javascript-while-loop-1cgj
JavaScript: While Loop - DEV Community
July 15, 2020 - Every loop has three key items: ... For example, we might want to log numbers from 1 to 10. Here, the start is 1, the end is 10, and the counter increments by 1 every time. let i = 1; // start while (i <= 10) { // end console.log(i); // 1 2 3 4 5 6 7 8 9 10 i++; // increment/counter }
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ Jsref โ€บ jsref_while.asp
JavaScript while Statement
Using continue - Loop through a block of code, but skip the value 3: let text = ""; let i = 0; while (i < 5) { i++; if (i == 3) continue; text += i + "<br>"; } Try it Yourself ยป ยท while is an ECMAScript1 (JavaScript 1997) feature.
๐ŸŒ
TutorialsTeacher
tutorialsteacher.com โ€บ javascript โ€บ javascript-while-loop
while loop in JavaScript
Unlike for loop, while loop only requires condition expression. ... Make sure condition expression is appropriate and include increment or decrement counter variables inside the while block to avoid infinite loop.
๐ŸŒ
Medium
jontzavala.medium.com โ€บ learn-about-javascript-loops-without-jumping-through-hoops-9a6e74e9b574
Learn About Javascript Loops Without Jumping Through Hoops | by Jonathan Zavala | Medium
October 10, 2021 - โ€œdo/while loopโ€ that will run once even if the condition is false at first. Also make sure you increase the variable by incrementing with the โ€œi++โ€ because if not you will get stuck in a loop that never ends and crash you browser.
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ loop-in-javascript-until-a-condition-is-met
JavaScript While Loop: How to Loop Until a Condition is Met (Troubleshooting Your Code Example) โ€” javaspring.net
Fix: Use the right increment/decrement logic. For 1โ€“5, increment by 1: ... Problem: The loop never runs because the initial condition is false. ... let score = 10; while (score > 20) { // score is 10, which is NOT > 20. Loop never runs!
๐ŸŒ
Kirupa
kirupa.com โ€บ html5 โ€บ loops_in_javascript.htm
For, While, and Do...While Loops in JavaScript
The condition checks if the value of i is less than 10. Is 0 less than 10? Yes it is, so this condition evaluates to true and the code contained inside the loop runs. Once this is done, the step part of our loop kicks in. In this stage, the i variable is incremented by 1 to have a value of 1.
๐ŸŒ
TechOnTheNet
techonthenet.com โ€บ js โ€บ for_loop.php
JavaScript: For Loop
For example, you could use counter++ to increment the counter by 1 or you could use counter-- to decrement the counter by 1. ... The statements of code to execute each pass through the loop. You would use a for loop when you want to execute the loop body a fixed number of times.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_loop_while.asp
JavaScript While Loop
The while loop loops through a block of code as long as a specified condition is true.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Operators โ€บ Increment
Increment (++) - JavaScript - MDN Web Docs - Mozilla
July 8, 2025 - The increment (++) operator increments (adds one to) its operand and returns the value before or after the increment, depending on where the operator is placed.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ javascript
Loop - increment - JavaScript - The freeCodeCamp Forum
June 3, 2021 - Dear Friends, I am wondering if I correctly understand how in below example increment (I) works. As I understood when (i) checks name โ€œAkiraโ€ i== 0 and when program goes down and checks โ€œHarryโ€ (i) increments and equals i==1 end so forth. is my reasoning correct? please can someone confirm if i correctly understood how i works **Your code so far** // Setup var contacts = [ { "firstName": "Akira", "lastName": "Laine", "number": "0543236543", "likes": ["Piz...
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ javascript for loop increment | final expression
JavaScript for loop increment example | Final Expression - EyeHunts
September 14, 2021 - We use the += or i++ assignment operator to update the counter variable. Itโ€™s called a final expression and work is to evaluate at the end of each loop iteration. This occurs before the next evaluation of condition.