JavaScript의 removeEventListener() 메서드는 addEventListener() 메서드를 사용해 요소에 등록했던 이벤트 리스너를 제거할 때 사용됩니다.
removeEventListener() 메서드란?
이 메서드는 더 이상 필요하지 않은 이벤트 핸들러를 정리(해제)하여 불필요한 동작을 방지하고 성능을 최적화하는 데 유용합니다. 단, 제거하려면 addEventListener()로 등록할 때 사용한 것과 동일한 함수 참조를 인자로 전달해야 한다는 점에 유의하세요. 익명 함수로 등록한 리스너는 나중에 제거할 수 없습니다.
사용 예제
다음 코드는 removeEventListener() 메서드의 동작을 보여줍니다.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.sample{
font-size: 18px;
font-weight: 500;
color:red;
}
</style>
</head>
<body>
<h1>JavaScript removeEventListener() method</h1>
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above button to print a number
</h3>
<button class="Btn">Remove event Listener</button>
<h3>Click on the above button to remove the event listener of the first button</h3>
<script>
let sampleEle = document.querySelector('.sample');
let j=0;
function printNum(){
sampleEle.innerHTML += 'Number = ' + j++ + '<br>';
}
document.querySelector('.Btn').addEventListener('click',printNum);
document.querySelectorAll('.Btn')[1].addEventListener('click',()=>{
document.querySelector('.Btn').removeEventListener('click',printNum);
sampleEle.innerHTML = 'Event listener has been removed';
})
</script>
</body>
</html>실행 결과
위 코드를 실행하면 다음과 같은 화면이 출력됩니다.

'CLICK HERE' 버튼을 세 번 클릭한 경우 - 클릭할 때마다 숫자가 순차적으로 출력됩니다.

'Remove event Listener' 버튼을 클릭한 후 다시 'CLICK HERE' 버튼을 클릭한 경우 - 첫 번째 버튼의 이벤트 리스너가 제거되었기 때문에 더 이상 숫자가 출력되지 않습니다.

핵심 정리
- removeEventListener()는 addEventListener()로 등록된 이벤트 리스너만 제거할 수 있습니다.
- 제거 시에는 등록 당시 사용한 것과 동일한 이벤트 타입과 함수 참조를 전달해야 합니다.
- 익명 함수(arrow function 등)로 등록한 리스너는 참조가 없어 제거할 수 없으므로, 제거가 필요한 경우에는 반드시 이름 있는 함수로 등록하세요.