Array.prototype.indexOf use of SameValue()

# John-David Dalton (16 years ago)

In the ECMA 5 RC spec I noticed that 15.4.4.14 Array.prototype.indexOf (and lastIndexOf) make use of the SameValue operation. The spec states that (9.12) SameValue will return true If Type(x) is Undefined or Null.

Would this not cause an issue with 9c of the Array.prototype.indexOf spec. "c. If SameValue(searchElement,elementK) is true, return k.".

[1,2,null].indexOf(null) -> SameValue(null, 1) -> true, return 0.

var undef; [1,2,undef].indexOf(undef) -> SameValue(undefined, 1) -> true, return 0.

# Allen Wirfs-Brock (16 years ago)

The first step of SameValue is

  1. If Type(x) is different from Type(y), return false.

undefined, null, and number are 3 distinct types so SameValue(null,1) -> false and SameValue(undefined,1) -> false

# Mark S. Miller (16 years ago)

On Tue, Apr 28, 2009 at 11:34 AM, John-David Dalton <john.david.dalton at gmail.com> wrote:

In the ECMA 5 RC spec I noticed that 15.4.4.14 Array.prototype.indexOf (and lastIndexOf) make use of the SameValue operation. The spec states that (9.12)  SameValue will return true If Type(x) is Undefined or Null.

The steps which say these (steps 2 & 3) occur after step 1:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.

so steps 2 & 3 only apply if Type(x) is the same as Type(y).

Would this not cause an issue with 9c of the Array.prototype.indexOf spec. "c. If SameValue(searchElement,elementK) is true,  return k.".

[1,2,null].indexOf(null) -> SameValue(null, 1) -> true, return 0.

No, because Type(null) is Null and Type(1) is Number; so step 1 will return false.