JavaScript Operators – Comparison vs. Assignment Operator

javascript

This is a piece of code from a simple Bookmarker App I made. I am kind of confused about something here.
Look at the 3rd line of the code. Isn't there supposed to be == instead of = after classname?
Because = is an assignment operator. What I need is true which == or === should give and it indeed does from the console.log.

However when I use === inside the if statement the function no longer works. But it works with the = which is not making any sense to me. It would be great if someone could clarify what's the problem here.

If anyone would like to check the full code including the HTML and CSS, here it is: https://github.com/magnetickode/Bookmarking-App

document.querySelector('.bookmarks').addEventListener('click', deleteBookmark);

function deleteBookmark(e) {

  if (e.target.className = 'delete') {

    e.target.parentNode.parentNode.removeChild(e.target.parentNode);

    console.log(e.target.className === 'delete');
    //console.log(e.target.parentNode.getElementsByTagName('p')[0].textContent);

    for (let i = 0; i < bookmarks.length; i++) {
      if (bookmarks[i].name === e.target.parentNode.getElementsByTagName('p')[0].textContent) {
        bookmarks.splice(i, 1);
        break;
      }
    }
  }
}

Best Answer

change this:

if (e.target.className = 'delete') {

to

if (e.target.classList.contains('delete')) {