skip to Main Content

I have been picking up JavaScript recently, and was wondering what the proper syntax would be for setting multiple variables for each outcome in a shorthand ternary.

Here is an example of what I currently have, a shorthand ternary that changes 1 variable if a condition is met, and changes 1 other variable if it is not.

var numberA = 5;
let rltvToTen = '';
let otherVar = '';

numberA >= 10 ? rltvToTen = 'EqualOrGreater' : rltvToTen = 'Less';

console.log(`Relative to ten: ${rltvToTen}`);

What I’m wondering is how I could make this so that if numberA >= 10, it would still set rltvToTen to the string ‘EqualOrGreater’ but would, in that same step, set otherVar to the string ‘Lorem Ipsum.’

For example, for condition?ifTrue:ifFalse, I want to set multiple actions to occur for ifTrue

(Note: I’m aware that other tactics could be applied, but I’m genuinely curious as to whether there is a proper syntax that allows multiple actions set to occur given a condition back-to-back in the shorthand form.)

(Another note: I’m towards the end of the day and running on my last braincell, so I don’t doubt I may have phrased this in an unnecessarily confusing way, and I’m sorry for that.)

I tried just putting the actions back to back.
I tried , and ; to separate multiple actions.
I tried using a space to separate multiple actions.
I tried reading the relevant documentation.

The answer to this is likely right in front of me, but I probably just can’t see it cuz I’m a skiddie newgen lmao.

3

Answers


  1. Chosen as BEST ANSWER

    edit: my bad, I'm blind. may have mentioned I'm running on my last braincell. my previous solution did, in fact NOT work. I was reading the loremipsum in output and failed to see the "undefined" returning for the first one. sorry @JaromandaX


  2. Added this only because there’s a really bad answer – which, despite protestation, does not work in ANY javascript environment, if you must do this, do it like this

    using grouping () and comma , operators

    var numberA = 12;
    let otherVar = '';
    let rltvToTen = '';
    numberA >= 10 ? (otherVar = 'loremipsum', rltvToTen = 'EqualOrGreater') : rltvToTen = 'Less';
    console.log(`Relative to ten: ${rltvToTen} other variable: ${otherVar}`)

    Though a more concise method is:

    var numberA = 12;
    let otherVar = '';
    let rltvToTen = numberA >= 10 ? (otherVar = 'loremipsum', 'EqualOrGreater') :'Less';
    console.log(`Relative to ten: ${rltvToTen} other variable: ${otherVar}`)

    But as mentioned in the comments, there’s nothing wrong with if for easy to read and understand code

    Login or Signup to reply.
  3. You could always return and destructure an array of values to one-line the assignment

    const numberA = 5;
    
    const [rltvToTen, otherVar] = numberA >= 10 ? ['EqualOrGreater', 'loremipsum'] : ['Less', ''];
    
    console.log({ rltvToTen, otherVar });
    const numberA = 12;
    
    const [rltvToTen, otherVar] = numberA >= 10 ? ['EqualOrGreater', 'loremipsum'] : ['Less', ''];
    
    console.log({ rltvToTen, otherVar });
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search