0

I'm trying to make multiexpression for ternary operator in Javascript like this :

condition ? 
exp1 ; ... ; expN 
:
alternative1 ; ... ; alternativeN

Here my code :

  validateEmail && validatePassword ?
        this.setState({validateForm: true})
        this.setState({nameSubmit:"onSubmit"})
        : null

My console returns me an error :

Declaration or statement expected

The javascript documentation talks about :

Syntax :

condition ? expr1 : expr2

But maybe someone has achieved the multi expression ternary operator ?

Thanks

Webwoman
  • 10,196
  • 12
  • 43
  • 87
  • You want to create a shortcut for "switch()" ? Because the classic boolean condition only knews two possible outcomes. :-) – JanRecker Jul 18 '18 at 19:12
  • And what should it evaluate to? `exp1` or `expN` or something inbetween? – Jonas Wilms Jul 18 '18 at 19:18
  • @JonasW. He's not trying to do multiple conditions, just multiple expressions in the true and false cases. – Barmar Jul 18 '18 at 20:08
  • The real question is *why*? Ternary operators should be used for short conditions and results, they become unreadable if you try to put too much in them. Use `if` for anything complicated. – Barmar Jul 18 '18 at 20:09
  • @JonasW. Oops, my comment was meant for JanRecker. – Barmar Jul 18 '18 at 20:45
  • @JanRecker He's not looking for `switch`, he only has one condition, multiple results. – Barmar Jul 18 '18 at 20:45
  • @JonasW. Notice also that his example expressions are mainly side-effecting, not value-returning. He just seems to be using the ternary as a replacement for `if`. – Barmar Jul 18 '18 at 20:47
  • @barmar hence my answer :) – Jonas Wilms Jul 18 '18 at 20:47

3 Answers3

2
const exp = () => { exp1; ... ; expN }
const alt = () => { alternative1; ... ; alternativeN }

condition ? exp() : alt();

It's not very readable you could use an IIFE to create the function and execute it in the same line.

condition ? (() => { exp1; ... ; expN })() : (() => { alternative1; ... ; alternativeN })();

Edit: I think you just added your code. In your code, I would recommend simply merging your setState calls into one command:

this.setState({
  validateForm: true, 
  nameSubmit:"onSubmit"
});
Aidan Hoolachan
  • 2,285
  • 1
  • 11
  • 14
  • thanks for the hints, concerning the second form with coma, my console returns me "second argument expect to be a function" – Webwoman Jul 18 '18 at 22:56
  • Ah, yes I needed to add an arrow function. EdIted answer – Aidan Hoolachan Jul 18 '18 at 23:18
  • my mistake, I would mean for this.setState({ validateForm: true, nameSubmit:"onSubmit" }); – Webwoman Jul 18 '18 at 23:24
  • 1
    I'm positive that that line of code is acceptable, so it must be coming from elsewhere. The full line would be ----- validateEmail && validatePassword ? this.setState({ validateForm: true, nameSubmit:"onSubmit" }) : null ------- Here's a similar question: https://stackoverflow.com/questions/40249934/updating-objects-with-setstate-in-react – Aidan Hoolachan Jul 18 '18 at 23:38
1

You can use the comma operator.

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

let a = 0,
    b = 1,
    c = 2,
    d = a === 0 ? (b++, c++) : -1;
    
console.log(b, c, d); // prints 2 3 and 2(because this will be the last expression to be evaluated).
Ele
  • 33,468
  • 7
  • 37
  • 75
1

To execute multiple statements in one branch there is the if statement:

if(validateEmail && validatePassword) {
   this.setState({validateForm: true});
   this.setState({nameSubmit:"onSubmit"});
}
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151