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
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › while
while - JavaScript - MDN Web Docs - Mozilla
let n = 0; while (n < 3) { n++; } console.log(n); // Expected output: 3 ... An expression evaluated before each pass through the loop. If this condition evaluates to true, statement is executed.
🌐
DEV Community
dev.to › bhagatparwinder › javascript-while-loop-1cgj
JavaScript: While Loop - DEV Community
July 15, 2020 - Every loop has three key items: Loop start · Loop end · Loop increment/decrement/counter · 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; ...
🌐
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.
🌐
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....
🌐
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. while (condition) { // code block to be executed } In the following example, the code in the loop will run, over and over again, as long as a variable (i) ...
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.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Loops: while and for
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 ...
🌐
Tutorial Republic
tutorialrepublic.com › javascript-tutorial › javascript-loops.php
JavaScript While, Do-While, For and For-In Loops - Tutorial Republic
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 increase by 1 each time the loop runs: ... The for loop is particularly useful for ...
🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-while-loop
while loop in JavaScript
JavaScript includes while loop to execute code repeatedly till it satisfies a specified condition. 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. As you can see in the above example, while loop will execute the code block till i < 5 condition turns out to be false.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript while loop
JavaScript while Loop By Examples
November 15, 2024 - The i variable is a loop variable. Second, add value of i to the total as long as i is less than or equal to 100: while (i <= 100) { total += i; i++; }Code language: JavaScript (javascript) Note that you need to increment i by one in each iteration.
🌐
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
Repeat: Check 2 <= 5 (true), print 2, increment to 3. Continue until count becomes 6: 6 <= 5 is false, so the loop exits. To avoid mistakes, it’s critical to understand the execution flow of a while loop:
🌐
CodeScracker
codescracker.com › js › js-while-loop.htm
while loop in JavaScript with examples
// Set a starting value for the counter variable let counter = 1; // While the counter is less than or equal to 5 while (counter <= 5) { // Print the value of the counter console.log(counter); // Increment the counter by 1 counter++; } ... The while loop can be used to repeatedly run a block ...
🌐
Techaltum
tutorial.techaltum.com › javascript-loops.html
Javascript loops | while, do while and for loop | Nested Loops
December 26, 2025 - If loop is not initialized, this will create syntax error. The while loop starts with while keyword and condition in parenthesis. while condition says that keep repeating the code block, until i is less than or equal to 10. i++ is step value, which is increment by 1 in this case.
🌐
Sling Academy
slingacademy.com › article › iterating-until-conditions-are-met-using-while-loops-in-javascript
Iterating Until Conditions are Met Using while Loops in JavaScript - Sling Academy
Inside the loop, we print the current ... and then increment it by 1 using counter++. Once counter reaches 5, the condition evaluates to false, and the loop terminates. While loops are versatile and can be utilized in various scenarios, such as reading user input, processing data until a condition ...
🌐
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 - The “while loop” will run through ... “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. Hopefully this keeps you from searching the internet too much about loops in javascript, but always ...
🌐
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