Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Currently having an issue with my If else function in JavaScript. The if statement works but the else statement does not

Hi so this is a very basic issue that I'm currently not able to solve. Using only JavaScript I'm trying to create a like button that changes it text content from an empty heart to a full heart while also having an increment and decrement functionality. I am using unicode as the text content.

My issue is that the if statement seems to be working just fine since on a click it does go from an empty heart to a full heart while also incrementing, but the else statement doesn't seem to be. It just seems to be stuck in the full heart and only keeps incrementing on click.

I've tried using else if as well but it doesn't seem to work either.

Any advice is appreciated.

let likes = Math.floor(Math.random() * 100)+1;
const likesDiv = document.createElement("span");
const likesButton = document.createElement("button");
likesDiv.appendChild(likesButton);

const emptyHeart = '\u2661';
const fullHeart = '\u2665';
likesButton.textContent = emptyHeart;
likesDiv.innerText = `${likes}`;

likesButton.addEventListener("click", toggle)
    
    function toggle() {
      if (likesButton.textContent = emptyHeart) {
        likesButton.textContent = fullHeart
        likes++;
        likesDiv.innerText = `${likes} `;
      }
      else{
        likesButton.textContent = emptyHeart;
        likes--;
        likesDiv.innerText = `${likes} `;
      }
    };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Comparisons are done with double equal sign ==. A single equal sign assigns the value to the variable and returns the assigned value. JavaScript will auto-cast this into a boolean, resulting in stepping into the if block for all truthy values.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!