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

140
Views
Can I make this code cleaner in Javascript?
var isFilterOn = false;

$('#toggle-filter-btn').on('click', function(e){
        e.preventDefault();
        if(!isFilterOn){
            isFilterOn = true;
        }else{
            isFilterOn = false;
        }
 })

Suppose I have an above piece of code.

Now, I was hoping to make the part where I set the isFilterOn to true or false, more concise.

So I tried this,

var isFilterOn = false;

$('#toggle-filter-btn').on('click', function(e){
        e.preventDefault();
        isFilterOn == false ? isFilterOn = true : isFilterOn = false; 
 })

But I am suspecting there is a better way to do this ?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Just assign the result of inverting the truthyness:

$('#toggle-filter-btn').on('click', function(e){
        e.preventDefault();
        isFilterOn = !isFilterOn;
});
about 4 years ago · Juan Pablo Isaza Report

0

your code can be a bit clean in this way

var isFilterOn = false;

$('#toggle-filter-btn').on('click', function(e){
        e.preventDefault();
        // your way
        isFilterOn == false ? isFilterOn = true : isFilterOn = false; 
        // little clean version
        isFilterOn = isFilterOn?false:true;
        // best option use revers boolean operator
        isFilterOn = !isFilterOn 
 })
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!