10

What does this line parent && (this.parent.next = this); mean? It just looks like its sitting there, doing nothing, not an if statement or a promise or anything. Is there a name for this style of coding?

    var Particle = function(i, parent)
{
    this.next = null;
    this.parent = parent;
    parent && (this.parent.next = this);
    this.img = new Image();
    this.img.src = "http://www.dhteumeuleu.com/images/cloud_01.gif";
    this.speed = speed / this.radius;
}

Its in multiple places in this animation file I'm looking at. Here's another example.. (!touch && document.setCapture) && document.setCapture();

this.down = function(e, touch)
{
    e.preventDefault();
    var pointer = touch ? e.touches[0] : e;
    (!touch && document.setCapture) && document.setCapture();
    this.pointer.x = pointer.clientX;
    this.pointer.y = pointer.clientY;
    this.pointer.isDown = true;
Jose Ricardo Bustos M.
  • 7,298
  • 6
  • 36
  • 56
Femtosecond
  • 624
  • 6
  • 17
  • See also [What is “x && foo()”?](http://stackoverflow.com/q/6970346/1048572) and [Is && statement() the same as if() statement()?](http://stackoverflow.com/q/12664230/1048572) – Bergi Apr 16 '15 at 00:58

2 Answers2

16

It's shorthand for

if (parent) {
    this.parent.next = this
}
tymeJV
  • 99,730
  • 13
  • 150
  • 152
  • 2
    A very maintenance-friendly shortcut at that. – Eric J. Apr 15 '15 at 19:51
  • 4
    not really much shorter – Legends Apr 15 '15 at 19:52
  • Still cool, I didn't know about this. – Scott Apr 15 '15 at 19:52
  • 1
    Exactly. In javascript any expression can be evaluated logically. Since && is a binary operator with short-circuit, if the first expression evaluates to false (there are several things that in javascript evaluate to that) then the right part of the && is not executed. – Claudio Apr 15 '15 at 19:53
  • 1
    How is it an advantage? It's only 3 characters shorter? – SRing Apr 15 '15 at 19:54
  • 18
    There is no advantage. It just makes it harder to follow when others maintain/read your code. – Peter Apr 15 '15 at 19:56
  • 1
    I guess minifiers would use this kinda shorthand – gitsitgo Apr 15 '15 at 20:01
  • 1
    The advantage is that it's one line instead of three, a single expression instead of a statement with sub-statement, and a less visually complex syntactic form. In other words, it makes it **easier** to follow when you or others maintain/read your code. – Leushenko Apr 15 '15 at 23:30
  • 2
    @Leushenko `if(parent) this.parent.next = this;`. There: now it is one line and less visually complex than that weird boolean expression. Since assignment is fundamentally a statement (it creates side effects), it is a bad idea in most cases to try to shoehorn it into an expression, so that last point is moot. – Asad Saeeduddin Apr 17 '15 at 21:12
7

if parent is false then not eject (this.parent.next = this), example:

parent = false;
parent && alert("not run");

Short-Circuit Evaluation:

As logical expressions are evaluated left to right, is named "short-circuit" evaluation,

variable && (anything); // anything is evaluated if variable = true.
variable || (anything); // anything is evaluated if variable = false

it's possible to use for assignment of variables:

var name = nametemp || "John Doe"; // assignment defaults if nametemp is false
var name = isValid(person) && person.getName(); //assignement if person is valid
Jose Ricardo Bustos M.
  • 7,298
  • 6
  • 36
  • 56