Javascript
-
Array.prototype.at으로 마지막 배열 값 쉽게 구하기Javascript 2023. 7. 13. 11:32
at은 비교적 최근에 추가 된 Array의 메서드다. 배열에서 해당하는 인덱스의 값을 반환해 준다. 대괄호 표기법(array[1])과 무엇이 다르냐 할 수 있는데, at 메서드의 강점은 음수를 넘겨줄 경우 배열의 뒤에서 부터 인덱스를 구한다는 것이다. const animals = ['cat', 'dog', 'cow']; console.log(animals[0]); // 'cat' console.log(animals.at(0)); // 'cat' console.log(animals[-1]); // undefined console.log(animals.at(-1)); // 'cow' // 배열에 없는 인덱스일 경우 undefined를 리턴한다. console.log(animals.at(3)); // unde..