return b.each(function () {
jQuery(this).hasClass("free-content") && (c = 0)
}), c
b.each
loops over all entries in b
, and checks whether the current element has the class free-content
set. Only if that yields true, the second part of the expression is evaluated – because they are concatenated via &&
, and JavaScript stops evaluating such an expression when the first part is false, because then the whole expression can’t become true any more.
So if there is an element with that class, the second part is evaluated – and thereby c
is set to the value 0
(because that’s the assignment operator =
there, and not a comparison).
And after that each
loop is finished, the value of c
is returned to the outside – because each()
and c
are connected via the comma operator ,
here, which evaluates its operands from left to right and then “returns” the second one.
JavaScript allows a lot of “fancy” coding like this, but as Marcus already said in his answer, that is hard to read, and there is no actual advantage here over, say
b.each(function() {
if(jQuery(this).hasClass("free-content")) {
c = 0;
return false;
}
});
return c;
I added the return false
here inside the each
loop, because once we found an element with that class, we don’t need to search the rest of them any more – that’s something that the person who came up with the original code forgot to do in all their “fancy-ness” … their loop will continue to iterate over all of b
’s elements, not matter if it finds the element it is looking for in the first round already.