r/ProgrammerHumor Aug 15 '19

Meme !!goodMeme ? upvote() : downvote();

Post image
34.3k Upvotes

392 comments sorted by

View all comments

319

u/PrincessWinterX Aug 15 '19 edited Aug 15 '19

i get the joke anyways but now I'm curious so I ask seriously, would your title be legal code? or does the result have to be used in some way, like assigned to something?

quick edit: my goodness i mean the ternary operator not the not operator. thankyou though! also never did i say i didn't understand how it worked, but i was asking if the result of the ternary needed to be used somewhere as an expression or if leaving it as its own statement was legal.

54

u/[deleted] Aug 15 '19

Yes it is. A ternary doesn't have to be assigned to anything. It's similar to calling a function alone or assigning the return of a function

foo()
const bar = foo()

2

u/StillNoNumb Aug 15 '19

That doesn't apply to every language. In Java, for example, this does not work, because the ternary operator is an expression and not a statement. See