I’m working with this JS plugin, and I’ve encountered some syntax I’ve never seen before. I understand what it’s doing, but I’m not sure why it works.
Here’s an example of one instance of it:
settings.maxId != null && (params.max_id = settings.maxId);
Is this just taking advantage of conditionals and the single = ? Is this common syntax for JS?
In JavaScript the
=operator is an expression and evaluates the assigned value. Because it is an expression it can be used anywhere an expression is allowed even though it causes a side-effect.Thus:
Means: If
settings.maxIdis not null then (and only then, since&&is short circuiting) evaluate the right-expression (params.max_id = settings.maxId) which in turn causes the value ofsettings.maxIdto be assigned toparams.max_id.This is much more clearly written as:
Happy coding.