I have to perform an IF statement in my Javascript code. I utilised the first method shown below:
(someVar1 !== "someString") && (someVar2[i].disabled = (someVar3.find("." + someVar4).length == 0));
Is the shortening of an if like this ok to do if my code is to be viewed by other members of my team? I have been told that it is a bit unclear and should instead use something like the following instead:
if (someVar1 !== "someString") {
var bool = someVar3.find("." + someVar4).length == 0;
someVar2[i].disabled = bool;
}
What are people's thoughts on this? Was it reasonable for me to implement the first method? Was it fair for me to be told I should change it and not use the (someVar) && doThis version of an if?
someVar && doThisis sometimes okay. In your case, I'd definitely go with option #2 – Jan Dvorak Feb 24 at 12:45!!boolean-coercion trick (x.disabled = !!someVar3.find("." + someVar4).length) if you wanted. But I'd also stick with option #2 - it's more easily readable. – Flambino Feb 24 at 13:09someVar3.find("." + someVar4).length == 0is supposed to represent. Choose a speaking name for the Boolean variable like maybehasNoExtension. – Olivier Jacot-Descombes Feb 24 at 18:57