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
🌐
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.
🌐
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.
🌐
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.
🌐
W3Schools
w3schools.com › jsref › jsref_dowhile.asp
JavaScript do...while Loop
The do...while is used when you want to run a code block at least one time. If you use a variable in the condition, you must initialize it before the loop, and increment it within the loop. Otherwise the loop will never end.
🌐
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....
🌐
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.
Find elsewhere
🌐
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 }
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Loops: while and for
But here we have the postfix form i++. So it increments i to 5, but returns the old value. Hence the comparison is actually while(4 < 5) – true, and the control goes on to alert. The value i = 5 is the last one, because on the next step while(5 < 5) is false. ... For each loop write down ...
🌐
W3Schools
w3schools.com › js › js_looping.asp
JavaScript Loops
The do...while loop is similar to the while loop, but guarantees that the code block will be executed at least once, before the condition is checked.
🌐
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.
🌐
Tutorial Republic
tutorialrepublic.com › javascript-tutorial › javascript-loops.php
JavaScript While, Do-While, For and For-In Loops - Tutorial Republic
increment — it updates the loop counter with a new value each time the loop runs. The following example defines a loop that starts with i=1. The loop will continued until the value of variable i is less than or equal to 5. The variable i will ...
🌐
W3Schools
w3schools.am › js › js_loop_for.html
JavaScript for Loop
If statement 2 returns true, the ... This will crash your browser. Read about breaks in a later chapter of this tutorial. Often statement 3 increments the value of the initial variable....
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › loops-in-javascript
JavaScript Loops - GeeksforGeeks
Example: The below JavaScript program do-while loop prints "Iteration:" followed by i, increments i by 1, and repeats the process while i is less than 3, ensuring the block runs at least once.
Published   January 19, 2026
🌐
W3Schools
w3schools.com › jsref › jsref_continue.asp
JavaScript continue Statement
let text = ""; let i = 0; while ... · More examples below. The continue statement breaks one iteration (in the loop) if a specified condition occurs, and continues with the next iteration in the loop....
🌐
Alma Better
almabetter.com › bytes › tutorials › javascript › do-while-for-while-loop-in-javascript
JavaScript for, while, and do-while Loops
June 22, 2023 - The increment/decrement statement is executed after each iteration of the loop and is used to update the loop variable. This process is repeated until the condition becomes false.
🌐
Medium
medium.com › @francesco-saviano › 10-exercises-with-while-loops-in-javascript-8cfb2bd81a71
10 Exercises with While Loops in JavaScript | by Francesco Saviano | Medium
October 24, 2024 - ... Initialize a counter variable ... the current value of the counter variable. Increment the counter variable by 1 after each iteration to ensure that the loop progresses....