본문 바로가기

프로그래밍/Javascript

Node v16.6.0 변경점 Array.prototype.at

이번 업데이트를 통해 V8 engine 9.2.xx가 적용되면서

Node에서도 Array.prototype.at을 사용할 수 있게 되었다. (String과 모든 TypedArray들도 마찬가지)

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at

 

Array.prototype.at() - JavaScript | MDN

The at() method takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.

developer.mozilla.org

기존

const arrays = ['a','b','c','d'];

// b를 얻으려면
console.log(arrays[1]);

// 마지막 값을 얻으려면
console.log(arrays[arrays.length - 1]);

 

at

const arrays = ['a','b','c','d'];

// b를 얻으려면
console.log(arrays.at(1));

// 마지막 값을 얻으려면
console.log(arrays.at(-1));

 

 

 

음수 인덱스로 찾아가는 것이 가능해졌다.

 

공식 문서를 보니 at의 추가는 성능적인 문제로 탄생했다기보다는 가독성면에서의 장점을 위해 추가된 기능이라고 합니다.