Javascript operator && as operator?

This should be a fairly simple question that I simply could not find anywhere, so I thought that I would save time and ask here.

I looked at javascript code and noticed a form expression:

a < b && (other maths statements);

Other maths statements are simply assigning variables and simple things.

My question is that. Is this another way to write compact if statements?

I know this if-block statement: else-block; this is one way to do this, but as && is short-circuited and left-associative, that’s all I can imagine. I know that you cannot have an else block with this method, but it is pretty neat.

Can someone just confirm that I'm awake at this time in the morning, please.

Thanks,

+4
2

, . a b, .

, , , . , , , . , , , , , , , ​​ .

, , "" . :

a < b && (a = 1) && (b = 0) && (c = 3);

, a b, a 1, b 0 c 3. (b = 0) false (0 ), (c = 3) . , , JavaScript, , , a, b c , , .

+7

if-else , , , :

JavaScript

var el = document.getElementById('test'),
a = 1,
b = 0;

a>b && (el.innerHTML = "True") || (el.innerHTML = "False");

HTML

<div id="test"></div>

jsFiddle

, , "False", "test" DIV.

+1

All Articles