最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is this JS syntax? Assignment in expression? (x != null && (y = x)) - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Mar 15, 2012 at 19:41 user166390 asked Mar 15, 2012 at 19:10 DerekDerek 9671 gold badge8 silver badges16 bronze badges 4
  • hey this seems to be poor code - (!=) means not equal and checking if object is not null and the second condition (=) means assignment - I wonder why it is used in conditional statement in this instance. Cheers! – Tats_innit Commented Mar 15, 2012 at 19:13
  • @Tats_innit: Where's the conditional statement? This code is perfectly valid; it's just silly. – Lightness Races in Orbit Commented Mar 15, 2012 at 19:13
  • @LightnessRacesinOrbit Hiya Srry man I presumed conditional because it uses not null check, thanks and srry if I am wrong - Ah cool so its not just me it is actually a bad statement then! Cheers – Tats_innit Commented Mar 15, 2012 at 19:16
  • i remember this coding style was popular in ANSI C for reading a file: while (nextChar = file.readLine()), but i dont think i have seen it since because it is an excellent anti-pattern. – jbabey Commented Mar 15, 2012 at 20:39
Add a comment  | 

3 Answers 3

Reset to default 15

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:

settings.maxId != null && (params.max_id = settings.maxId)

Means: If settings.maxId is 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 of settings.maxId to be assigned to params.max_id. This is much more clearly written as:

if (settings.maxId != null) {
  params.max_id = settings.maxId
}

Happy coding.

The && operator is known as "boolean AND". Typically, you'd see it in an if statement:

if (x == true && y == false) {

but that's not a restriction. You may use it in any valid expression to "combine" the boolean values of its operands into a single boolean result, according to the logical "AND" operation:

var z = (x == true && y == false);
// z is now true or false, accordingly

One of the lovely things about && is that it "short circuits". In false && true, because the first operand is false the entire expression may only evaluate to false, so the second operand is not even evaluated.

Let's check that again:

var z = (false && foo());
// z is now false

In this statement, the function foo is never even called! It doesn't have to be, for the program to know that z will be false.

This is more than an optimisation — you can rely on it.

Some silly people use this technique to rewrite conditional statements:

if (x == 0) {
   foo();
}

into hard-to-read single expressions:

(x == 0) && foo();

Now, consider that assignment can be an expression just like a function call:

var a = (b = c);

Or:

var a = (b = foo());

And add in a conditional via the above technique:

var a = ((x == 0) && (b = foo()));

Now the entire expression b = foo() won't be evaluated at all if x is not 0, because of short circuiting.

We don't even need to do anything with the result of the && operation, and if we don't store it to a you're left with just:

(x == 0) && (b = foo());

which is a statement that'll assign b to the value of foo() only if x is 0.

Avoid it. It's hard to read. Just use an if statement.

this statement will assign params.max_id = settings.maxId only if settings.maxId != null due to the fact that && is a short-circuit logic operator

this behaviour is due to the fact that javascript will evaluate the condition until it's necessary. thus, if first condition is false and the second is in AND there's no need to check further

发布评论

评论列表(0)

  1. 暂无评论