Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
132
rated 0 times [  135] [ 3]  / answers: 1 / hits: 98993  / 13 Years ago, mon, may 9, 2011, 12:00:00

Is there any reason I should use string.charAt(x) instead of the bracket notation string[x]?


More From » string

 Answers
10

Bracket notation now works on all major browsers, except for IE7 and below.



// Bracket Notation
Test String1[6]

// charAt Implementation
Test String1.charAt(6)


It used to be a bad idea to use brackets, for these reasons (Source):




This notation does not work in IE7.
The first code snippet will return
undefined in IE7. If you happen to use
the bracket notation for strings all
over your code and you want to migrate
to .charAt(pos), this is a real pain:
Brackets are used all over your code
and there's no easy way to detect if
that's for a string or an
array/object.



You can't set the character using this notation. As there is no warning of
any kind, this is really confusing and
frustrating. If you were using the
.charAt(pos) function, you would not
have been tempted to do it.



[#92319] Sunday, May 8, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marint

Total Points: 550
Total Questions: 105
Total Answers: 124

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
;