Warning on Javascript Increment (++) Operator

Version: 6.1.2 - build 1421

The following warning is shown:

[attachment=0]recordCount…png[/attachment]
for the code:

        var countRecords = 0
        
        for ( var i = 1; i <= records.length; i++ )
        {
                ...
            countRecords++
                ...
        }

However, if I change the code to:

        var countRecords = 0
        
        for ( var i = 1; i <= records.length; i++ )
        {
                ...
            countRecords = countRecords + 1
                ...
        }

no warning is issued. I would think either code style should work. Is this a bug, or am I not understanding something?

Not a bug, it only means that you are declaring a value, and assigning it, but never use it.
Look for the occurences of this variable AFTER the code you’ve shown: I bet you never really use it anywhere else, and in the loop it is just useless.

Using a variable means that it assigned to something (on the left hand side of an assignment), or that you are using it for a test for example.

In your case:

countRecords = countRecords + 1

is a use of the countRecords, but since you are not using it elsewhere, this code does nothing anyway, so you are just masking the warning saying that countRecords is never used, by assigning it to something.

Ah, got it…the warning mechanism is looking for a left-hand side assignment (which the increment operator is not). Thank you, Patrick, for the explanation.