6

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?

2
  • 5
    let me quote from the code comment just above the for loop: // Assign all array values to 0 Commented Jan 17, 2016 at 16:29
  • It is helpful for code golf. For example this saves 2 bytes because you don't need a code block wrapped in brackets {}: for(a=0;a++<100;b=a,c=b+a); Commented Jan 17, 2016 at 18:42

7 Answers 7

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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).

3 Comments

Have you tested it? I'm pretty sure it is not faster.
yeah, it's because in a[i++]=0 i++ (increment and getting the value of i) are processed as one instruction, whilst in a[i]=0; i++; they are two; but as i said, premature optimizations. Every function-call or deoptimized function, and so much more has a way higher impact on performance than this optimization.
What "instruction" do you mean? You cannot just count JS tokens.
1

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);

4 Comments

you have strange definition of make sense - if(a===0) b=0; or even b = (a===0 ? 0 : b) are both more readable IMO
Obviously not a recommended practice nor something I'd suggest nor something that I'd write, but an answer to the question. There are assignments in conditionals, probably more than you'd expect to see. Coming up with more readable variations would be irrelevant and exhausting here. Focus on the question.
please allow me to add an emphasis to the OP's question: "real examples that one should use empty statements"
Sorry, I either missed that part, or it was edited while I was writing the answer.
1

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.

Comments

1

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.

Comments

0

The first example for(var i = 0; i < a.length; a[i++] = 0); is useful IMO, and the reasons would be:

  • Writing less without sacrificing readability.
  • beauty!
  • Telling people: Hey, I'm a pro JS coder. :)

The second one if((a==0) || (b == 0)); seems doesn't nothing.

Comments

0

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() );

2 Comments

No. You would write if (X()) Y(); (or X() && Y();). No empty statement.
Why not X() && Y()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.