How to fix the error "Uncaught SyntaxError: Unexpected token else"?

Good day to all!
There is a list of links
<div class='links'>
 <a href='#'>link 1</a>
 <a href='#'>link 2</a>
 <a href='#'>link 3</a>
</div>

By clicking on the link, you need to add her background as the selected element.
Means JS added class= "selected" to the tag <a> when clicked.
let active = document.getElementsByTagName("a") [0];
active.onclick = function() {
 if (active.classList.remove('selected'));
this.classList.add('selected');
else
active.classList.remove('selected');
}


But if add else get error.
Tell me, what's wrong with the code?
April 7th 20 at 15:27
2 answers
April 7th 20 at 15:29
if (active.classList.remove('selected'));
Remove the ; at the end!
April 7th 20 at 15:31
let active = document.getElementsByTagName("a") [0];
active.onclick = function() {
if (active.classList.remove('selected')) {
this.classList.add('selected');
} else {
active.classList.remove('selected');
}
}
probably because it is necessary

Find more questions by tags JavaScript