공부 정리/웹(Web)
[javascript] string.charAt(i) vs string[i] 비교
경적필패.
2022. 9. 8. 22:49
반응형
학습 이유
문자열을 인덱스로 접근할 때 두 방법에 차이가 있었다.
학습 내용
"abc".charAt(0) ==> a
"abc".charAt('dsafdsfasdf') ==> a
"abc"[0] ==> a
"abc"['asdfadsfa'] ==> undefined
"abc".charAt(5) ==> ""
"abc"[5] ==> undefined
한 줄 정리
charAt()은 범위 밖에 벗어난 인덱스를 찾을 경우, 공백 출력(숫자 말고 다른 걸 입력하면 그냥 첫 글자를 출력함)
[index] 방식은 undefined 출력
참고 : https://thisthat.dev/string-char-at-vs-string-bracket-notation/
반응형