Reputation: 3137
I tried to search for good resources on empty statement, but it seem like nothing show up. Even on MDN, they don't have much to say about it.
i.e:
for(var i = 0; i < a.length; a[i++] = 0);
if((a==0) || (b == 0));
I would like to know what are some real examples that one should use empty statements on their project. What are the reason behind it?
Upvotes: 6
Views: 2218
Reputation: 3593
none/lazyness. there is absolutely no difference to
for(var i = 0; i < a.length;) a[i++] = 0;
and just a minimal difference to
for(var i = 0; i < a.length; i++) a[i] = 0;
the first one is a few ms faster after a few billion iteration steps; aka. premature optimization
EDIT:
if((a==0) || (b == 0));
this makes no sense at all, since it does nothing.
but expresions like
a==0 || (b=0);
//or maybe sth like this:
//var noop = ()=>void 0; //FYI
typeof a === "function" || (a = noop);
are pretty useful to me, since they are short, and readable and an additional if-statement doesn't add any value to readability or understanding (at least once you know this pattern).
Upvotes: 2
Reputation: 8446
My favorite use for it is to wait for a condition to become true.
while ( !condition );
// do what happens once your condition is met
This is nice to read, in my opinion, but the same can be done with { }
instead of the empty statement.
Upvotes: 1
Reputation: 1171
Let us suppose you have two functions X
and Y
and let us suppose that Y
must only be executed when X
returns true
, in such a situation you will write:
if( X() && Y() );
Upvotes: 0
Reputation: 665101
The examples you've given don't make much sense. They should better be written
for (var i = 0; i < a.length;) a[i++] = 0;
for (var i = 0; i < a.length; i++) a[i] = 0;
; // the comparisons really don't do anything (assuming a and b are no objects)
(a==0) || (b = 0); // Oh wait, that's the one given by @Shomz
if (a != 0) b = 0;
However, there are real-world applications for the empty statement. I'll just list 3 that come to my mind:
function x() {
…
};
A semicolon where it doesn't belong (e.g. after the function declaration above) makes an empty statement.
;
…
A leading semicolon on your script files helps to guard against buggy inclusions or file concatenations.
while (!check_for_finish()); // do nothing
An empty loop body can be used for busy-waiting loops (not recommended) and similar.
Upvotes: 2
Reputation: 29285
The first example for(var i = 0; i < a.length; a[i++] = 0);
is useful IMO, and the reasons would be:
The second one if((a==0) || (b == 0));
seems doesn't nothing.
Upvotes: 0
Reputation: 3818
I do not think that they are really useful, but I can be wrong. One can try to use side effects of the evaluation of the conditions in an if, but I do not see a good reason to do so.
Upvotes: 1
Reputation: 37701
The first one obviously loops through the array and assigns all values to zero, without having the code specified in the statement.
The other one seems like a typo, because it is useless.
However, something like
if((a==0) || (b = 0));
would make sense, as it would assign b
to zero in case a
is not zero.
var a = 1, b = 1;
if((a == 0) || (b = 0));
alert("a: " + a + ", b: " + b);
Upvotes: 1