boomchickawawa
boomchickawawa

Reputation: 566

Multiple if condtions in a single statement

I am trying to check if a username and userId exists and either a like or a comment exists then return something. And I am using two if conditions for this :

if (username && userID) {
  if (like || comment) {
    return something
  }
}

Is there any way to fit bot the conditions into one single statement. Also, here the username is immutable.

Upvotes: 0

Views: 40

Answers (2)

AM Douglas
AM Douglas

Reputation: 1903

Yup!

if (username && userID && (like || comment)) {
  // do stuff
}

Upvotes: 2

CertainPerformance
CertainPerformance

Reputation: 370729

Just surround both conditions with brackets so you can group them with &&?

if (
  (username && userID) &&
  (like || comment)
) {
  return something
}

Upvotes: 3

Related Questions