How to get nth character in a string in Javascript?

Let me summarize the possibilities

given:

var string = "HAI";
/* and */
var index = 1; // number ∈ [0..string.length), rounded down to integer
  • string.charAt(index) returns "A" (preferred)
  • string[index] returns "A" (non-standard)
  • string.substring(index, index+1) returns "A" (over-complex solution but works)
  • string.substr(index, 1) returns "A" (non-standard approach to above)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)