0

I know ternary operator with multiple statements works in React/JavaScript with:

condition ? (statement1,statement2,...) : (statement);.

Explored to get to know how it works.

Following is my code which is causing an error:

localProducts[productFoundAt].qty > 0 ? (localProducts[productFoundAt].qty-- , localCartedProducts[iterator].qty++) : alert("More quantity not available");

Error:

./src/reducers/reducer.js Line 26:21: Expected an assignment or function call and instead saw an expression no-unused-expressions

Search for the keywords to learn more about each error.

Similar code with if/else is working fine:

if (localProducts[productFoundAt].qty > 0) {
    localProducts[productFoundAt].qty--;
    localCartedProducts[iterator].qty++
} else {
    alert("More quantity not available");
}

What I am doing wrong here? Thanks for the help. Also why similar code working here?

let a = 10;
let b = 10;

a==b ? (a-- , b--):alert("Hello World");
console.log(a);
console.log(b);

10
  • 3
    Those operands of the comma operator are expressions, not statements. Commented Jan 29, 2020 at 14:01
  • 1
    The error looks like it's coming from ESLint, not JavaScript. Commented Jan 29, 2020 at 14:03
  • 4
    Your linter is complaining because you shouldn't use the conditional operator when you don't need an expression as the result anywhere. Just write proper readable code, using the if statement! Commented Jan 29, 2020 at 14:03
  • 1
    @Justinas It is valid, it is just an unreadable mess. Commented Jan 29, 2020 at 14:04
  • 1
    @ZainUlAbideen EsLint doesn't say it's not valid, it complains that it is bad style. That's exactly the job of a linter. Commented Jan 29, 2020 at 15:12

1 Answer 1

1

That's a valid syntax. It is just an es-lint error which you can ignore by adding the following line:

/* eslint-disable no-unused-expressions */
localProducts[productFoundAt].qty > 0 ? (localProducts[productFoundAt].qty-- , localCartedProducts[iterator].qty++) : alert("More quantity not available");

Hope this works for you.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks buddy, it's working fine. Can you please help me understanding the reason?
We just bypassed the rule of es-lint that was producing the warning.
I know why it worked, but I wanna know why it was not working given that it was a valid statement?
It was working! Lint rules are established to catch common areas of code that are easy to create baked in bugs, or lead to less readable code, or any other number of reasons. It is very opinionated. Somewhere in a lint config that rule is configured to be an error. You can modify it to only be a warning (so builds succeed), remove the rule all together, or keep it and add the disable override for those times when want to use a ternary like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.