상세 컨텐츠

본문 제목

.at()

JavaScript

by 폴리프레임 2024. 6. 18. 20:14

본문

반응형

at()은 정수를 받고, 정수의 인덱스 값을 반환한다.

 

String.at()

const sentence = 'The quick brown fox jumps over the lazy dog.';

let index = 5;

console.log(`An index of ${index} returns the character ${sentence.at(index)}`);
// "An index of 5 returns the character u"

index = -4;

console.log(`An index of ${index} returns the character ${sentence.at(index)}`);
// "An index of -4 returns the character d"

Array.at()

const array1 = [5, 12, 8, 130, 44];

let index = 2;

console.log(`An index of ${index} returns ${array1.at(index)}`);
// "An index of 2 returns 8"

index = -2;

console.log(`An index of ${index} returns ${array1.at(index)}`);
// "An index of -2 returns 130"

'JavaScript' 카테고리의 다른 글

clsx, twMerge, cn, shadcn-ui  (0) 2024.07.05
toJSON(), JSON.stringify()  (1) 2024.06.18
scrollHeight, scrollTop  (0) 2024.06.18
TextDecoder()  (0) 2024.06.14
ArrayBuffer, TypedArray, Int8Array, Unit8Array  (1) 2024.06.14

관련글 더보기