Javascript ternary operator

Hi all, can anyone explain this line of code?

r = ret = oid ? [oid] : [];

can anyone explain it using if statement?

If variable oid contains truthy value it will create array which contains value from oid variable, if it is not true it will return empty array and it will be assigned to variable ret whose value will be assigned to variable r.

On the end of the day r will contain array with one element [oid] or it will contain empty array [].

Explanation with if statement:

if (oid) {
  r = ret = [oid];
} else {
  r = ret = [];
}

Thank you very much

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.