JavaScript에서 함수는 객체이므로 다른 함수에 매개변수로 전달할 수 있습니다. 이러한 함수는 다른 함수 내에서 호출될 수 있으며 전달된 함수를 콜백 함수라고 합니다.
다음은 JavaScript 콜백을 위한 코드입니다 -
예시
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
</style>
</head>
<body>
<h1>JavaScript Boolean constructor Property</h1>
<p class="sample"></p>
<button class="btn">CLICK HERE</button>
<h3>
Click on the above button to call the add function which calls back
another function
</h3>
<script>
function add(a, b, callback) {
callback(a + b);
}
function multiplyResultByTwo(res) {
document.querySelector(".sample").innerHTML = res * 2;
}
document.querySelector(".btn").addEventListener("click", () => {
add(4, 5, multiplyResultByTwo);
});
</script>
</body>
</html> 출력

"여기를 클릭" 버튼을 클릭하면 -
