The difference between str [0] and str.charAt (0)

What is the difference between str[0]and str.charAt(0)? I always access a specific character by simply typing str[i]where iis the index of the character I want to access (counted from 0), but last week I had a good overview of open source JS code and in every single project I seen, people use the method charAt.

Is there a difference between the two?

+4
source share
2 answers

[] - A more primitive way to access all types of arrays.

charAt() refers to strings.

[], charAt() .

, .

charAt(), , undefined IE7

, , charAt() undefined, ""

+5

""[-4]          // undefined

"".charAt(-4)   // ""

, .

+2

Source: https://habr.com/ru/post/1534006/


All Articles