2021-04-26 17:29:13 +02:00
Most checks against an ``++indexOf++`` call against an array compare it with -1 because 0 is a valid index. Any checks which look for values >0 ignore the first element, which is likely a bug. If you're merely checking the presence of the element, consider using ``++includes++`` instead. Before using ``++includes++`` method make sure that your browser version is supporting it.
2020-06-30 12:48:07 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-06-30 12:48:07 +02:00
----
var color = "blue";
var name = "ishmael";
var arr = [color, name];
if (arr.indexOf("blue") > 0) { // Noncompliant
// ...
}
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-06-30 12:48:07 +02:00
----
var color = "blue";
var name = "ishmael";
var arr = [color, name];
if (arr.indexOf("blue") >= 0) {
// ...
}
2021-04-26 17:29:13 +02:00
if (arr.includes("blue")) {
2020-06-30 12:48:07 +02:00
// ...
}
----
== See
2021-04-27 10:40:29 +02:00
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes[Array.prototype.includes()] documentation at MDN
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]