What does expression && expression syntax mean? [duplicate]
Asked Answered
F

2

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;
Felike answered 15/4, 2015 at 19:49 Comment(1)
H
16

It's shorthand for

if (parent) {
    this.parent.next = this
}
Hammond answered 15/4, 2015 at 19:50 Comment(9)
A very maintenance-friendly shortcut at that.Isidora
not really much shorterPregnable
Still cool, I didn't know about this.Alb
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.Scyphozoan
How is it an advantage? It's only 3 characters shorter?Sailesh
There is no advantage. It just makes it harder to follow when others maintain/read your code.Stranglehold
I guess minifiers would use this kinda shorthandPharisaic
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.West
@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.Eikon
C
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
Cobia answered 15/4, 2015 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.