0
var filterBy = window.location.search.substring(10);
if(filterBy !==""){
    if(filterBy ==='all'){
        $('#filterItems').find('[data-type]').show();
    }else{
        $('#filterBy li').removeClass('active');
        $('#filterBy').find('li[data-filter= "'+ filterBy + '"]').addClass('active');
        $('#filterItems').find('[data-type]').hide().end().find('[data-type*="' + filterBy + '"]').show();
    }
}

I want to make it optimized. filterBy==="" and filterBy === all. I feel its checking two times for condition. I want to have only condition check

2 Answers 2

4

If filterBy equalt to "all", is not empty. So you don't need to check it too.

if (filterBy === 'all') {
    // your code here
} else if (filterBy !== '') {
    // your code here
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply do this.

if (filterBy === "" || filterBy === 'all'){
   // if condition is met
} else {
  // if condition is not met
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.