6

Possible Duplicate:
Check variable equality against a list of values
Javascript: Comparing SINGLE Value Against MULTIPLE Values with OR Operands

First of all I am new to javascript side. Please let me know is there any simple way of formating the below code.

if( fileName== 'doc' || fileName=='docx' || fileName=='xls' || fileName=='xlsx' || fileName=='ppt' || fileName=='pdf' ){

Do something

} else{

    Do something

}
Community
  • 1
  • 1
user1817630
  • 135
  • 4
  • 1
    I would be keeping an array of possible file extensions, and then checking if the value was in that array. – Brad Jan 08 '13 at 15:41

6 Answers6

10
var SUPPORTED_FILE_EXTENSIONS = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pdf'];

if(SUPPORTED_FILE_EXTENSIONS.indexOf(fileName) != -1){
    //Do Something
} else {
    //Do Something Else
}

Array.indexOf() - Note that this also includes information about adding backward compatibility support for older browsers (I.E. IE).

Briguy37
  • 8,342
  • 3
  • 33
  • 53
  • Tnx @briguy37 :) . It will be nice if you explain this 'FILE_SUPPORTED_EXTENSIONS.indexOf(fileName) != -1' – user1817630 Jan 08 '13 at 15:43
  • 3
    Notice that `indexOf` is not supported by old IEs – Bergi Jan 08 '13 at 15:44
  • 1
    SUPPORTED_FILE_EXTENSIONS is the array; .indexOf checks for the occurrence of a string (filename) and returns it's index position unless it's not found then it returns -1. – Syon Jan 08 '13 at 15:46
  • 1
    @user1817630: `indexOf` on an array will give you `-1` if the array does not contain the first argument you provide. Thus, in this case that check will give you a value of `true` when `fileName` is in the supported file extensions. – Briguy37 Jan 08 '13 at 15:50
9
var validTypes = /docx?|xlsx?|ppt|pdf/;

if (fileName.match(validTypes)) {
    ...
}

Note: the question mark, ?, in the Regular Expression indicates the previous character is optional so it can match both doc and docx the pipe, |, indicates or so it will match doc or xls, etc.

JaredMcAteer
  • 21,688
  • 5
  • 49
  • 65
3

maybe something like

var filenames = ['doc','docx','xls','xlsx','ppt','pdf'];
if(filenames.indexOf(fileName) >= 0 ) {
  // Do something
} else {
    // Do something else
}
user160820
  • 14,866
  • 22
  • 67
  • 94
1

Maybe switch statement is one of the fastest options:

switch (fileName) {
    case "doc":
    case "docx":
    ...
        // ...
    default:
        // else
}
VisioN
  • 143,310
  • 32
  • 282
  • 281
0

You could reformat it to make the code easier to read:

if( fileName == 'doc' || 
    fileName == 'docx' || 
    fileName == 'xls' || 
    fileName == 'xlsx' || 
    fileName == 'ppt' || 
    fileName == 'pdf' ){
    Do something
} else{
    Do something
}

Alternatively, you could make the code less repetitive using a construct such as jQuery.inArray() or array.indexOf():

var arr = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pdf'];
if (jQuery.inArray(fileName, arr) > -1){
    Do something
} else{
    Do something
}
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
0

Creating the array is easier to read. Plus use jQuery's $.inArray() method. It works just like .indexOf() with the added benefit that it's compatible with older version of IE which have a lot of missing array functions.