42 lines
948 B
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
If you have an iterable, such as an array, set, or list, your best option for looping through its values is the ``++for of++`` syntax. Use a counter, and ... well you'll get the right behavior, but your code just isn't as clean or clear.
In a browser environment, `NodeList` and other array-like collections should work by default. If you are using TypeScript and seeing a type error, make sure your configuration is correct.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
const arr = [4, 3, 2, 1];
for (let i = 0; i < arr.length; i++) { // Noncompliant
console.log(arr[i]);
2021-04-28 16:49:39 +02:00
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
const arr = [4, 3, 2, 1];
for (let value of arr) {
console.log(value);
2021-04-28 16:49:39 +02:00
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]