B
B
Barrakuda742018-02-19 09:47:20
JavaScript
Barrakuda74, 2018-02-19 09:47:20

Why does this code work and how to correctly return values ​​in return according to the conditions?

Please explain this code

var x = undefined;
var y = true;
function test(x, y) {
  var z = '';
    var count = 0;
  return  1 == x && (z += "взяли x<br>", count += 1),
          1 == y && (z += "взяли y<br>", count += 1),
          z += 'Кол-во переменных: ' + count,
          z
}
  document.write(test(x,y)); // выведет 'взяли y Кол-во переменных: 1'

Why does it work, what is this construct in return?
I know what this code does, if x == 1, then the code that comes next in brackets after && will be executed. But why is there a '&&' here?? Isn't it 'I'? And why doesn't the construction look like a classic shorthand, i.e.: return boolean ? 'foo' : 'bar';

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-02-19
@Stalker_RED

Why does it work, what is this construct in return?
Because it is correct and syntactically correct code without errors. Somewhat confusing, but that doesn't stop it from working properly.
Yes, you are right &&- this is a logical AND. And there are also commas
And why doesn't the design look like a classic...
Actually a very strange question. This thing looks different because it was written differently.
Your K.O.
It looks like someone has achieved quite good results in the art of writing cryptic mind-blowing code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question