ms_nitrogen ms_nitrogen - 4 months ago 9
Javascript Question

if statement == at least one of multiple options

I am trying to figure out the proper syntax of how to write an if statement that checks if

x == (at least one of many vars)


I have an example what what I am trying to get to...

var path = require('path')
var fs = require('fs')
var file1 = './frank.txt'
var file2 = './jasmine.html'
var file3 = './finnigan.jpg'

if (fs.statSync(file2).isFile && path.extname(file2) == ['.txt' || '.html']) {
console.log('true')
} else {
console.log('failure')
}


I know the syntax on the after is && is weird, but what is the correct syntax for the situation?

Answer

You can check if a certain index exists by the elment:

if (fs.statSync(file2).isFile && ['.txt', '.html'].indexOf(path.extname(file2)) > -1) {
  console.log('true');
} 
else {
  console.log('failure');
}