Question

I recently read a javascript code online and want to convert it to C#. When encountered this line of code, I had no idea what it does: X = (!X ? 8 : X). Please explain.

Was it helpful?

Solution

It checks whether or not X is false (contains either 0, empty string, null, or undefined). If it is, the line assigns 8 to X, otherwise X retains its original value.

OTHER TIPS

It sets the X var to 8 if !X is true (so if X is false). Otherwise, X keep the same value.

So, if X is an empty string, the false boolean or the 0 number (I might forget some values but well you understand), it'll be set to 8, else it'll keep its original values. It's the same as:

if(!X) { X = 8 } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top