I know that there are better ways to check for an empty string—bear with me.
This is not checking for empty string in general. I’m actually doing something like:
var s = /*who knows?*/;
switch (s.charAt(0)) {
// ...
}
and wanted to know if I can avoid having to do an extra if (!s).
Yes, as long as the value is a valid string. Section 15.5.4.4 of the language spec says
That was not changed since ES3. Interpreters are pretty good around string function compatibility.
That said, I recently implemented a peephole optimization for
charAtto Closure compiler but I did not optimize out of bounds checks because the compiler tends not to optimize what are considered programmer errors.