I know that the .at method is already in Stage 4 and it is not the right time to talk about its specification, but I decided to create this topic because its specification is very very harmful.
The reason this happens is that the argument goes through ToIntegerOrInfinity. Instead, it should go through ToNumber and then validated with IsIntegralNumber.
I know that it has already been shipped to the browsers, but could you please consider fixing it once again?
This is how almost all functions in JavaScript behave. Compare the very similar arr.slice("foo") or arr.slice(1.5).
It's (in my opinion) unfortunate that JS is so aggressive about doing type coercion rather than rejecting unreasonable arguments, but it's a very, very strong precedent at this point, and it would probably be more confusing to have only some functions work that way.
.at should not be equated with Array#slice. Since arr.at(-1) is introduced as a syntax sugar for arr[arr.length - 1], it should be equated with general property access.
It is not in fact a syntax sugar for arr[arr.length - 1] - [arr.at](http://arr.at)(n) is precisely syntax sugar for arr.slice(n)[0], and it behaves accordingly.
I understand what you are saying, but what JavaScript users want is a syntax sugar for arr[n >= 0 ? n : n + arr.length], not a syntax sugar for arr.slice(n)[0].
If you don't want to change it, please enlighten us that arr.at(n) is not a syntax sugar of what we want. I don't think many people really understand what that is.
The difference is only observable if you're passing something which is not an integral number. But if you've ended up with a non-integral number as an argument to .at, you almost certainly have a bug anyway.
This behavior is also the ecosystem precedent. Compare, for example, lodash's nth method, which works exactly the same way. I don't think this has ever actually caused problems in practice. (E.g., there don't seem to be any issues on the lodash repo expressing surprise at this behavior.)
I don't know how many people have used _.nth, but I suspect it's a small percentage of JavaScript users. I've used lodash, but I didn't know that method existed.
Since there are users who are currently unhappy with Array#at, could you please think about making it more usable, instead of getting hung up on consistency for no reason?
I'm sure there's a bias and I don't think it's enough people answering, but don't you think it's weird that the current spec makes misleading to such a large percentage of people?
OK. I've thought about it a lot, and it seems to be correct that a argument of a method named at should be rounded to an integer with ToIntegerOrInfinity, just like the similarly named String#charAt and String#charCodeAt. Indeed, it makes sense.
Syntax sugar for property access should be proposed in other ways.
Since all of javascript numbers are floats, then they would be susceptible to rounding errors, so in some ways, it can actually be a good thing that it rounds.
I also agree that it should behave more like the slice() function. Yes, at() mimics array indexing in some ways, but it's also a function, not an operator, and so I would want it to be consistent with other functions.
In the end, we can disagree on this, but like has been previously pointed out, if you're trying to index with NaN, 'hello world', etc, then you've already got a bug in your program that needs to be sorted out. Fixing this type coercion issue with just a single function still leaves all of the other built in function susceptible to this kind of type coercion none-sense. The only real way to defend against it is to use something like typescript, not to try and change how the language operators after X point in time.
It looks plausible. However, in that code example, people might expect it to return 2 instead of 1. (Not that it matters, but the semicolon-less code apparently doesn't work as expected).
IMO, returning a seemingly correct value makes it harder to find bugs than returning nothing at all. And since it is impossible for TypeScript to determine whether a number is NaN or not, we have no way to fight it. In order to avoid creating bugs as much as possible, we should not rely on unstable methods such as Array#at. We should explicitly round numbers to integer values for property access.
What's being asked for here is a fundamental change to a stage 4 proposal that's already being shipped. The ship has already sailed. It's too late to fix this, even if we wanted to (which, not everyone seems to want this changed). People are already writing code that depends on this proposal.