Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of using empty statement in JavaScript?

Tags:

javascript

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?

like image 371
Bun Avatar asked Jan 17 '16 16:01

Bun


People also ask

What is the need of empty statement?

A statement is an instruction given to the computer to perform any kind of action. An empty statement is useful in situations where the code requires a statement but does not require logic. To fill these two requirements simultaneously an empty statement is used.

Is Empty value JavaScript?

Use the condition with “” and NULL to check if value is empty. Throw a message whenever ua ser does not fill the text box value.

What does an empty function return?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

What is empty statement explain with example?

An empty statement in Java is written as a single semicolon. The empty statement doesn't do anything, but the syntax is occasionally useful. For example, you can use it to indicate an empty loop body of a for loop: for(int i = 0; i < 10; a[i++]++) // Increment array elements /* empty */; // Loop body is empty statement.


3 Answers

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.

like image 143
Bergi Avatar answered Oct 09 '22 05:10

Bergi


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

like image 44
Thomas Avatar answered Oct 09 '22 07:10

Thomas


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);
like image 29
Shomz Avatar answered Oct 09 '22 06:10

Shomz