mark adlington
mark adlington

Reputation: 21

javascript - if and or (||) statement

if (window.location.pathname == "/" || window.location.pathname == "/yyy.asp") {
  runSlideShow();
}
if (
  window.location.pathname == "zzzz.html" ||
  window.location.pathname == "/xxx.html"
) {
  runSlideShow();
}

How do I merge the if statements so there is only one {runSlideShow();} rather than two;

currently I cant seem to have more than two window.location.pathname statements with an || in between.

Upvotes: 0

Views: 178

Answers (7)

Mitya
Mitya

Reputation: 34608

It's possible to both merge your conditions and cut down your code significantly.

if (['/', '/yyy.asp', 'zzzz.html', '/xxx.html'].includes(location.pathname))
    runSlideShow();

Or alternatively, for the hell of it, shorter still but less readable/scalable:

if (/^(?:(\/(yyy\.asp|xxx\.html)?)|zzzz\.html)$/.test(location.pathname))

Upvotes: 4

mark adlington
mark adlington

Reputation: 21

if (['/', '/yyy.asp', 'zzzz.html', '/xxx.html'].includes(location.pathname))
runSlideShow();

Thanks everybody, i have used this solution.

Upvotes: 0

Maielo
Maielo

Reputation: 732

You can have different approachto this problem

// set array with allowed paths
const pathsForSlideShow = ['/', '/yyyy.asp', 'etc...']; 
// check if path is in array if so run the slidshow
if ( pathForSlideShow.indexOf(window.location.pathname) !== -1 ) runSlideShow();

// or if you don't need to support IE
if ( pathForSlideShow.includes(window.location.pathname) ) runSlideShow();

Upvotes: 0

Sukanta Bala
Sukanta Bala

Reputation: 879

You can do it in this way:

if (["/", "/yyy.asp", "zzzz.html", "/xxx.html"].includes(window.location.pathname)) { 
  runSlideShow();
}

Or

if (window.location.pathname == "/" || window.location.pathname == "/yyy.asp" || 
window.location.pathname == "zzzz.html" || window.location.pathname == "/xxx.html") 
{
  runSlideShow();
}

Upvotes: 0

Cameron Downer
Cameron Downer

Reputation: 2048

One way to keep your if condition as short and sweet as possible. Extract the conditions out to their own functions.

if (shouldRunSlideShow()) {
   runSlideShow();
}

function shouldRunSlideShow() {
    return currentPathMatchesAny(["/", "/yyy.asp", "/zzzz.html", "/xxx.html"]);
}

function currentPathMatchesAny(paths) {
    return paths.includes(window.location.pathname);
}

Quite verbose code but I think it reads well.

Upvotes: 0

KiaiFighter
KiaiFighter

Reputation: 667

There is no reason you shouldn't be able to have 4 conditional checks in a single if statement.

if (window.location.pathname=="/" || window.location.pathname=="/yyy.asp" || window.location.pathname=="zzzz.html" || window.location.pathname=="/xxx.html") {
  runSlideShow();
} 

Upvotes: 0

Josh Wulf
Josh Wulf

Reputation: 4877

if (["/", "/yyy.asp", "zzzz.html", "/xxx.html"].includes(window.location.pathname)) { 
  runSlideShow();
}

or, full gangsta:

["/", "/yyy.asp", "zzzz.html", "/xxx.html"].includes(window.location.pathname) && runSlideShow();

Upvotes: 0

Related Questions