I have a variable:
var text = "hello";
I want to get the 0 positioned character, so:
var firstChar = text[0];
Simple. In firefox and chrome this works. In IE however i always get back 'undefined'
Any ideas why this might be happening in IE?
Strings aren't accessible like arrays in IE (prior to IE9). Instead you can use charAt
, which is available cross-browser:
var text = "hello";
var firstChar = text.charAt(0);
// firstChar will be 'h'
You can use .substr()
.
var firstChar = text.substr(0,1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With