1

[wasn't sure how to word this question exactly, but] say I have some code:

var myValue = 6;
var newValue = myValue == 6 ? myValue : 3;

notice that I'm using the ternary operator to check if a variable fulfils a certain condition, and if so, then I set my newValue to that variable, and if not, something else.

So the (slight) problem:

In a case like this, I have to actually write the variable name twice (myValue == 6 ? myValue : ...), this usually isn't a problem, but say I have a very long variable name, or even the property of a certain array or something else I don't want to manually write out, or even make a new variable; I just want to set the new variable equal to the value of the variable in the condition.

If my condition is simply if the expression exists, then I could obviously do:

newValue = myValue || 3;

but if I'm checking any other condition, I have to write the checking-variable twice, so is there any way to access the ternary's condition text, at least, or even an array of the variables involved in the condition so I have some kind of hope to access it, or is there any other way to do one-line conditions like this, without assigning a new variable?

Yaakov5777
  • 309
  • 5
  • 15
  • If it's just a variable name, I wouldn't worry about writing it out twice - it's the most readable option you have. If it's a nested property of some sort, best to keep things DRY and extract it into a variable first. – CertainPerformance Mar 11 '19 at 07:36
  • @CertainPerformance yes possibly, but I usually like doing things in one-line ./ one expression without setting it to a variable – Yaakov5777 Mar 11 '19 at 07:41
  • Also, if you are worried about reading and *understanding* the code, then you can always extract the check into a variable. For example `myValue == 6` may mean very little (aside from the fact that it's an illustrative example), so you could extract that to `isCorrect = myValue == 6` and put that in the ternary which would mean it's more readable. You could even have it more descriptive as a `isRevolverFullyLoaded = bulletsInTheChamberOfRevolver == 6` as a slightly fictitious example. – VLAZ Mar 11 '19 at 07:41
  • @VLAZ but then I Would have to make another line / statement for the ternary condition, what if I wanted to do it all in one line/statement – Yaakov5777 Mar 11 '19 at 07:43
  • 1
    IMO, unless you're code-golfing, it's not something to worry about - and if you're golfing, creating another variable with a really short name is the way to do it. – CertainPerformance Mar 11 '19 at 07:45
  • @VLAZ Its mainly used for when I want to access the property of an object, but if the object doesn't exist then it gives the error "can't read x of undefined", so I want to set a value equal to the property of an unknown object, I first have to check if that object exists, then I can set it to the value, but it would be cool if I could just set it to the value of the "unknown" object directly – Yaakov5777 Mar 11 '19 at 07:45
  • @CertainPerformance see last comment – Yaakov5777 Mar 11 '19 at 07:46
  • 1
    Dupe of https://stackoverflow.com/questions/2631001/test-for-existence-of-nested-javascript-object-key ? – CertainPerformance Mar 11 '19 at 07:47
  • @CertainPerformance good find – Yaakov5777 Mar 11 '19 at 07:48

0 Answers0