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

134
Views
cleaner way to check whether or not a the current window.location.href is equal to any of the strings in the array

Im trying to do a redirect if the url is equal to any of the strings in the following array of urls:

So I did the following

function redirectUser() {
  if (window.location.href === 'https://swish.com/login' || window.location.href === 'https://swish.com/register' || window.location.href === 'https://swish.com/overview') {
    window.location.replace('https://swish.com/onboard');
  }
}

But this is a bit ugly, so I thought of putting the urls in an array and doing something like this:

function redirectUser() {
  const urls = ['https://swish.com/login', 'https://swish.com/register', 'https://swish.com/overview' ]
for(let url of urls) {
  if (window.location.href === url) {
    window.location.replace('https://swish.com/onboard');
  }
 }
}

Is there any other way to do this? If not, which would be the better option in your opinion? Thanks!

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

0

i think it will help you

   function redirectUser() {
     const urls = ['https://swish.com/login', 'https://swish.com/register', 
      'https://swish.com/overview' ]
         if(urls.includes(window.location.href)){
         window.location.replace('https://swish.com/onboard');
        }
     }  
about 4 years ago · Juan Pablo Isaza Report

0

It appears that you already have all the strings statically within the code. So you can try using switch which performance wise is faster than if check here.
If you are reproducing strings via some logic, then you can also opt for Regular Expressions which is even faster. Else, you can stick with arrays.

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!