wanderors
wanderors

Reputation: 2292

How to check if a variable exists and its value in one if loop in nodejs

I have to check if a variable exists and then if exists, then if its value is this do something. How can I make both in one single if loop

if (process.env.MODULE) {
  if (process.env.MODULE.includes("reRun")) {
    // Do something
  }
}

How can I combine both if loop. So if MODULE value is not defined, it should not return any error. If its defined and its value is reRun, then only it has to do something

Upvotes: 1

Views: 241

Answers (1)

spaceSentinel
spaceSentinel

Reputation: 670

You can combine two boolean expression using logical operators like && (AND) and || (OR)

if (process.env.MODULE && process.env.MODULE.includes('reRun)) {
    // do something
}

Upvotes: 2

Related Questions