Can't Use Index Operator on Strings in JavaScript

Posted Fri Aug 05 @ 03:48:20 PM PDT 2011

If you're using the index operator [] to get a char out of a string in JavaScript, your script will fail in Internet Explorer.

var str = "Hello World"; var e = str[1]; alert(e); // "undefined" in IE <= 7

You need to use the charAt method instead:

var str = "Hello World"; var e = str.charAt(1) alert(e); // "e"

<< Home