[JS] Array.prototype.at()

Park Inhye·2024년 1월 25일
0

Javascript

목록 보기
6/7
post-thumbnail

at(index)

  • 정숫값을 받아 해당 인덱스에 있는 항목을 반환
  • 양수 음수 둘 다 가능
  • 음수는 뒤에서부터 셈
  • index < -array.length 또는 index >= array.length인 경우
    • undefined 반환
  • 마지막 요소
    • arr.at(-1) == arr[arr.length - 1]
  • 뒤에서 n번째 값
    • arr.at(-3) == arr[arr.length - 3] == arr.slice(-3, -1)[0]
const array1 = [5, 12, 8, 130, 44];

let index = 2;

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

index = -2;

console.log(`An index of ${index} returns ${array1.at(index)}`);
// Expected output: "An index of -2 returns 130"
profile
👁👄👁

0개의 댓글