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.
what does "X = (!X ? 8 : X)" mean in javascript? [duplicate]
-
24-06-2022 - |
题
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.
解决方案
其他提示
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 }
不隶属于 StackOverflow