Jump to content

Need explanation of inline Ternary operator


Recommended Posts

I understand how Ternary operators work but i came across an inline one and im trying hard to figure out what each operation is doing. Can anyone please explain this to me: var hadRelations = false,isSure = false, presidentQuote = hadRelations ? "Did not have relations" : isSure ? "Did not have relations" : "I admit"; "I admit" is the value returned 1st i dont understand why they use "," instead of ";" to terminate variable definitions. But what is the Ternary doing? i don't understand the use of the first two strings in the operation. What is the concept here, what would make the return value equal to either of the first two strings, instead of the last one? Thanks

Link to post
Share on other sites

You can sayvar a;var b;var c;or you can sayvar a,b,c;or you can sayvar a, b, c; forvar answer = x ? y : z; if x is true then answer = yif x is false then answer = z forvar answer = x ? y : z ? a : b if x is true then answer = yif x is false and z is true then answer = aif x is false and z is false then answer = b

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...