# Loops should not be infinite (javascript:S2189)

**URL:** <https://community.sonarsource.com/t/loops-should-not-be-infinite-javascript-s2189/63685>\
**Category:** Report False-positive / False-negative...\
**Tags:** javascript\
**Created:** [May 23, 2022, 10:47am UTC](https://community.sonarsource.com/t/loops-should-not-be-infinite-javascript-s2189/63685 "2022-05-23T10:47:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcolmer](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/pcolmer/32/32131_2.png) [@pcolmer](https://community.sonarsource.com/u/pcolmer)\
**Post date:** [May 23, 2022, 10:47am UTC](https://community.sonarsource.com/t/loops-should-not-be-infinite-javascript-s2189/63685/1 "2022-05-23T10:47:43Z")

</div>

```auto
const getDatesBetweenDates = (startDate, endDate) => {
	let dates = [];
	const endDateObject = new Date(endDate);
	const iteratorDate = new Date(startDate);
    // Analyser complains about the next line
	while (iteratorDate < endDateObject) {
		dates = [...dates, new Date(iteratorDate)];
		iteratorDate.setDate(iteratorDate.getDate() + 1);
	}
	dates = [...dates, new Date(endDate)];
	let sortedDates = dates.sort((a, b) => a - b);
	console.log('Dates between:', sortedDates);
	return sortedDates;
};

```

The analyser is complaining that `iteratorDate` and `endDateObject` are not modified in the `while` loop, but `iteratorDate` is modified.

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [May 23, 2022, 12:04pm UTC](https://community.sonarsource.com/t/loops-should-not-be-infinite-javascript-s2189/63685/2 "2022-05-23T12:04:06Z")

</div>

Hey @pcolmer

As noted in the thread on [how to report a false-positive](https://community.sonarsource.com/t/how-to-report-a-false-positive-false-negative/37022), can you tell us what product you’re using and, if relevant, which version?

In any case, I think you are probably reporting this known false-positive which you can track on GitHub: [FP on S2189 when loop condition variables are modified through function calls · Issue #2866 · SonarSource/SonarJS · GitHub](https://github.com/SonarSource/SonarJS/issues/2866)

---

<div class="post-metadata">

**Author:** ![pcolmer](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/pcolmer/32/32131_2.png) [@pcolmer](https://community.sonarsource.com/u/pcolmer)\
**Post date:** [May 24, 2022, 6:35am UTC](https://community.sonarsource.com/t/loops-should-not-be-infinite-javascript-s2189/63685/3 "2022-05-24T06:35:41Z")

</div>

> [@Colin](#):
>
> As noted in the thread on [how to report a false-positive](https://community.sonarsource.com/t/how-to-report-a-false-positive-false-negative/37022), can you tell us what product you’re using and, if relevant, which version?

My apologies.

- SonarQube Community Edition
- Version 9.2.3 (build 50713)

Thanks for the link to the GitHub issue.
