Syom Syom - 1 year ago 91
Javascript Question

How to get first character of string?

I have a string, and I need to get its first character.

var x = 'somestring';
alert(x[0]); //in ie7 returns undefined


How can I fix my code?

Answer Source

What you want is charAt.

var x = 'some string';
alert(x.charAt(0)); // alerts 's'