JavaScript ternary operators without "Else"

I always had to put null in else conditions that have nothing. Anyway, around? For example.

 condition ? x = true : null; 

basically, is there a way to do:

 condition ? x = true; 

Now it appears as a syntax error

FYI, here is some real code example:

 !defaults.slideshowWidth ? defaults.slideshowWidth = obj.find('img').width()+'px' : null; 
+96
optimization javascript syntax-error
May 28 '10 at 9:50 p.m.
source share
7 answers

First of all, the triple expression does not replace the if / else construct - its equivalent to the if / else construct that returns a value. That is, the if / else clause is code, a three-dimensional expression is an expression , which means that it returns a value.

This means a few things:

  • use ternary expressions only when you have a variable on the left = to which the return value should be assigned
  • use only ternary expressions when the return value must be one of two values ​​(or use nested expressions if appropriate)
  • each part of the expression (after? and after :) should return a value without side effects (the expression x = true returns true, since all expressions return the last value, but also change x without x, which have any effect on the returned value)

In short, the "proper" use of ternary expression

 var resultofexpression = conditionasboolean ? truepart: falsepart; 

Instead of your example condition ? x=true : null ; condition ? x=true : null ; where you use a ternary expression to set the value of x , you can use this:

  condition && (x = true); 

This is still an expression and therefore cannot pass the test, so even a better approach would be

  void(condition && x = true); 

The latter will pass the test.

But then again, if the expected value is logical, just use the result of the condition expression itself

 var x = (condition); // var x = (foo == "bar"); 



UPDATE With respect to your sample, this is probably more appropriate:

 defaults.slideshowWidth = defaults.slideshowWidth || obj.find('img').width()+'px'; 
+167
May 29 '10 at 2:08 a.m.
source share

No, he needs three operands. That's why they are called ternary operators.

However, for what you have as an example, you can do this:

 if(condition) x = true; 

Although it’s safer to have braces if you need to add more than one statement in the future:

 if(condition) { x = true; } 

Edit: Now that you specify the actual code to which your question relates:

 if(!defaults.slideshowWidth) { defaults.slideshowWidth = obj.find('img').width()+'px'; } 
+16
May 28 '10 at 21:52
source share
 var x = condition || null; 
+10
May 28 '10 at 21:53
source share

More often, people use logical operators to shorten the syntax of statements:

 !defaults.slideshowWidth && (defaults.slideshowWidth = obj.find('img').width()+'px'); 

But in your particular case, the syntax might be even simpler

 defaults.slideshowWidth = defaults.slideshowWidth || obj.find('img').width()+'px'; 

This code will return defaults.slideshowWidth if defaults.slideshowWidth obj.find('img').width()+'px' is true and obj.find('img').width()+'px' otherwise case.

See Short Circuit Evaluation of Logical Operators for details.

+5
01 Oct. '15 at 7:29
source share

You can write

 x = condition ? true : x; 

So x is unmodified when the condition is false.

It is then equivalent

 if (condition) x = true 

EDIT:

 !defaults.slideshowWidth ? defaults.slideshowWidth = obj.find('img').width()+'px' : null 

There are several alternatives - I'm not saying it's better / worse - just alternatives

Passing at zero, as the third parameter works, because the existing value is null. If you reorganize and change a condition, then there is a danger that this is no longer the case. Transferring outbound value as a second choice in triple protection against this:

 !defaults.slideshowWidth = ? defaults.slideshowWidth = obj.find('img').width()+'px' : defaults.slideshowwidth 

It’s safer, but maybe not so nice to watch and type. In practice, I will probably write

 defaults.slideshowWidth = defaults.slideshowWidth || obj.find('img').width()+'px' 
+3
May 28 '10 at 21:54
source share

In your case, I see the triple operator as redundant. You can assign a variable directly to an expression using the ||, && operators.

 !defaults.slideshowWidth ? defaults.slideshowWidth = obj.find('img').width()+'px' : null ; 

will become:

 defaults.slideshowWidth = defaults.slideshowWidth || obj.find('img').width()+'px'; 

This is more understandable, this is a more "javascript" style.

+1
Sep 30 '15 at 10:34
source share

About just

  if (condition) { code if condition = true }; 
-one
Jan 11 '19 at 19:03
source share



All Articles