What is this JS syntax? Purpose in expression? (x! = null && (y = x))

I work with this JS plugin and I came across some syntax that I have never seen before. I understand what he does, but I'm not sure why he works.

Here is an example of one of them:

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

Is it just using conditional and single =? Is this common syntax for JS?

+4
source share
3 answers

In JavaScript, the = operator is an expression and evaluates the assigned value. Since this is an expression, it can be used wherever an expression is allowed, even if it causes a side effect. Thus:

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

Means: if settings.maxId not null (and only then, since && short circuiting ), evaluate the correct expression ( params.max_id = settings.maxId ), which in turn leads to the params.max_id settings.maxId for params.max_id . It is much more clearly written as:

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

Happy coding.

+14
source

this statement assigns params.max_id = settings.maxId only if settings.maxId != null due to the fact that && is a logical short circuit operator

this behavior is due to the fact that javascript will evaluate the condition to its necessity. thus, if the first condition is false and the second is in AND , there is no need to additionally check

+3
source

The && operator is known as "boolean AND ". Typically, you will see it in the if :

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

but this is not a limitation. You can use it in any real expression to "combine" the logical values โ€‹โ€‹of your operands into one logical result in accordance with the logical "AND":

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

One of the great things about && is that it is short circuits. In false && true , since the first operand is false , the whole expression can only be evaluated to false , so the second operand is not even evaluated.

Repeat one more time:

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

In this statement, the function foo never called! It is not necessary for the program to know that z will be false.

This is more than optimization - you can rely on it.

Some stupid people use this technique to rewrite conditional statements:

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

into hard-to-read single expressions:

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

Now consider that an assignment can be an expression similar to a function call:

 var a = (b = c); 

Or:

 var a = (b = foo()); 

And add the conditional value using the above method:

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

Now the whole expression b = foo() will not be evaluated at all if x not 0 due to a short circuit.

We do not need to do anything with the result of the && operation, and if we do not store it on a , you will remain with:

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

which is an operator that assigns b to foo() only if x is 0 .

Avoid this. Hard to read. Just use the if .

+3
source

Source: https://habr.com/ru/post/1401676/


All Articles