<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Document</title>
<style>
.block {
width: 200px;
height: 200px;
background-color: magenta;
display: none;
}
.block.open { display: block }
</style>
</head>
<body>
<button>кнопка</button>
<div class="block">asd</div>
<script>
(() => {
const block = document.querySelector('.block');
const button = document.querySelector('button');
function toggle() {
block.classList.toggle('open');
}
function remove() {
block.classList.remove('open');
}
document.addEventListener('click', e => {
const target = e.target;
target === button ? toggle() : target !== block ? remove() : false;
});
})();
</script>
</body>
</html>