This page was translated from English by the community.Learn more and join the MDN Web Docs community.
TypedArray.prototype.forEach()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2016년 9월.
forEach() 메서드는 주어진 함수을 형식화 배열에 있는 각 요소에 대해 한 번씩 실행합니다.이 메서드는Array.prototype.forEach()와 동일한 알고리즘으로 동작합니다.형식화 배열은형식화 배열 타입 중 하나입니다.
In this article
시도해 보기
const uint8 = new Uint8Array([10, 20, 30]);uint8.forEach((element) => console.log(element));// Expected output: 10// Expected output: 20// Expected output: 30구문
// Arrow functionforEach((element) => { /* ... */ } )forEach((element, index) => { /* ... */ } )forEach((element, index, array) => { /* ... */ } )// Callback functionforEach(callbackFn)forEach(callbackFn, thisArg)// Inline callback functionforEach(function(element) { /* ... */ })forEach(function(element, index) { /* ... */ })forEach(function(element, index, array){ /* ... */ })forEach(function(element, index, array) { /* ... */ }, thisArg)매개변수
callbackFn각 요소에 대해 실행할 함수.
다음 세 가지 매개변수를 받습니다.
thisArgOptionalcallbackFn을 실행할 때this로 사용할 값.
반환 값
설명
forEach()는 주어진callbackFn을 형식화 배열에 있는 각 요소에 대해 한 번씩 실행합니다.삭제했거나 초기화하지 않은 인덱스에 대해서는 실행하지 않습니다. 그러나undefined값을 가진, 존재하는요소에 대해서는 실행합니다.
callbackFn은 다음세 인수와 함께 호출됩니다.
- 요소 값
- 요소 인덱스
- 순회 중인 배열
thisArg 매개변수를forEach()에 제공한 경우callbackFn을 호출할 때 전달해this의 값으로 쓰입니다. 전달하지 않으면undefined를this 값으로 사용합니다.callbackFn이 최종적으로 관찰할 수 있는this 값은함수의this를 결정하는 일반적인 규칙에 따라 결정됩니다.
forEach()로 처리할 요소의 범위는 최초callbackFn 호출 전에 설정됩니다.forEach() 호출을 시작한 뒤배열에 추가한 요소는callbackFn이 방문하지 않습니다. 형식화 배열의 기존 요소값이 바뀐 경우,callbackFn에 전달하는 값은forEach()가 요소를 방문한 시점의 값을 사용합니다.방문하기 전에 삭제한 요소는 방문하지 않습니다.
forEach()는 각 형식화 배열 요소에 대해 한 번씩callbackFn 함수를 실행합니다.every()과some()와는달리undefined를 반환합니다.
예제
>형식화 배열의 컨텐츠 기록하기
아래 코드는 형식화 배열의 각 요소당 한 줄의 로그를 남깁니다.
function logArrayElements(element, index, array) { console.log(`a[${index}] = ${element}`);}new Uint8Array([0, 1, 2, 3]).forEach(logArrayElements);// Logs:// a[0] = 0// a[1] = 1// a[2] = 2// a[3] = 3명세서
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-%typedarray%.prototype.foreach> |