How to nest OR statements in JavaScript?

536 views Asked by At

Currently I am creating a filemanager.

What I want is to check if the user has selected a video file. The file can be mov, f4v, flv, mp4 and swf.

I want to check if my var ext is one of these.

What I have is:

if(ext == ('mov' || 'f4v' || 'flv' || 'mp4' || 'swf'))
{
    //Do something
}

Does anyone know how I can get this working. I don't want to use a switch because I will get a lot of cases.

3

There are 3 answers

1
jAndy On BEST ANSWER

You would need to explicitly compare the variable against each of those values.

if( ext === 'mov' || ext === 'f4v' || ... ) { 
}

..but, RegExp to the rescue, we can go like

if( /mov|f4v|flv|mp4|swf/.test( ext ) ) { 
}
0
Maddog On

Kinda nice way is this:

var exts = {
     "mov" : null,
     "f4v" : null,
     "flv" : null,
     "mp4" : null,
     "swf" : null,
}

if(ext in exts){
    // world peace
}
0
Reinder Wit On

you need to split them up, like so:

if(ext === "mov" || ext === "f4v" || ext === "flv" || ext === "mp4" || ext === "swf")
{
    // do stuff
}

you could also consider putting all the different extension in an array and checking whether ext exists in that array