Ternary operators in JavaScript without an “else”

I’ve always had to put null in the else conditions that don’t have anything. Is there a way around it?

For example,

condition ? x = true : null;

Basically, is there a way to do the following?

condition ? x = true;

Now it shows up as a syntax error.

FYI, here is some real example code:

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

11 Answers
11

Leave a Comment